-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial-array-i_3151.py
53 lines (29 loc) · 1.15 KB
/
special-array-i_3151.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
# You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
# Example 1:
# Input: nums = [1]
# Output: true
# Explanation:
# There is only one element. So the answer is true.
# Example 2:
# Input: nums = [2,1,4]
# Output: true
# Explanation:
# There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.
# Example 3:
# Input: nums = [4,3,1,6]
# Output: false
# Explanation:
# nums[1] and nums[2] are both odd. So the answer is false.
# Constraints:
# 1 <= nums.length <= 100
# 1 <= nums[i] <= 100
# ---------------------------------------Runtime 53 ms Beats 51.62% Memory 16.34 MB Beats 99.92%---------------------------------------
from typing import List
class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
for i in range(len(nums) - 1):
predicate: set[bool] = {nums[i] % 2 == 0, nums[i + 1] % 2 == 0}
if len(predicate) == 1:
return False
return True