-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode_2760_Longest_Even_Odd_Subarray_With_Threshold.py
62 lines (43 loc) · 2 KB
/
Leetcode_2760_Longest_Even_Odd_Subarray_With_Threshold.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
54
55
56
57
58
59
60
61
62
"""
You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions:
nums[l] % 2 == 0
For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2
For all indices i in the range [l, r], nums[i] <= threshold
Return an integer denoting the length of the longest such subarray.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3,2,5,4], threshold = 5
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Example 2:
Input: nums = [1,2], threshold = 2
Output: 1
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
Example 3:
Input: nums = [2,3,4,5], threshold = 4
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
It satisfies all the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= threshold <= 100
"""
#MY SOLUTION
class Solution:
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
i = 0
sz = maxi = 0
while i<len(nums):
if nums[i]%2==0 and nums[i]<=threshold:
start = i
while i < len(nums)-1 and nums[i] % 2 != nums[i + 1] % 2 and nums[i+1]<=threshold:
i += 1
sz = i-start+1
maxi = max(maxi,sz)
i += 1
return maxi