-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial-array-ii.py
30 lines (24 loc) · 1.15 KB
/
special-array-ii.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
from typing import List
from bisect import bisect_left, bisect_right
class Solution:
def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]:
# Determine if the first element is even or odd
is_even = nums[0] % 2 == 0
consecutive_errors = []
# Identify indices where the special condition is violated
for i in range(1, len(nums)):
if (is_even and nums[i] % 2 == 0) or (not is_even and nums[i] % 2 != 0):
consecutive_errors.append(i)
is_even = nums[i] % 2 == 0 # Toggle the expected parity
results = []
# Process each query
for start, end in queries:
# Use binary search to find the range of error indices within the query range
left_idx = bisect_left(consecutive_errors, start + 1)
right_idx = bisect_right(consecutive_errors, end)
# If there are errors in the range, the result is False; otherwise, True
if right_idx > left_idx:
results.append(False)
else:
results.append(True)
return results