2760. Longest Even Odd Subarray With Threshold
Question
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
Solution
temp记录当前窗口长度,res记录最大长度。
循环,如果当前数字大于threshold则清空temp。
否则如果当前temp长度为零,则判断当前位置是否为偶数,如是偶数长度加一。
如果当前temp长度不为零,则判断是否与前一个位置不同号。如果是则temp加一,如果为否则重置temp。
Code
1 | class Solution { |
2760. Longest Even Odd Subarray With Threshold
https://xuanhe95.github.io/2023/07/02/2760-Longest-Even-Odd-Subarray-With-Threshold/