-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2760-LongestEvenOddSubarrayWithThreshold.go
More file actions
103 lines (89 loc) · 4.3 KB
/
2760-LongestEvenOddSubarrayWithThreshold.go
File metadata and controls
103 lines (89 loc) · 4.3 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
// 2760. Longest Even Odd Subarray With Threshold
// 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:
// 1. nums[l] % 2 == 0
// 2. For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2
// 3. 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
import "fmt"
// Sliding window
func longestAlternatingSubarray(nums []int, threshold int) int {
res, i, n := 0, 0, len(nums)
max := func (x, y int) int { if x > y { return x; }; return y; }
for i < n {
if nums[i] % 2 == 1 || nums[i] > threshold {
i++
continue
}
start := i
for i < n - 1 && nums[i] % 2 != nums[i + 1] % 2 && nums[i + 1] <= threshold {
i++
}
i++
res = max(res, i - start)
}
return res
}
func longestAlternatingSubarray1(nums []int, threshold int) int {
res := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 0; i < len(nums); i++ {
start := i
if nums[start] % 2 == 0 && nums[start] <= threshold {
for ; i < len(nums) - 1 && nums[i]%2 != nums[i + 1]%2 && nums[i + 1] <= threshold; i++ {}
res = max(res, i - start + 1)
}
}
return res
}
func main() {
// 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.
fmt.Println(longestAlternatingSubarray([]int{3,2,5,4}, 5)) // 3
// 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.
fmt.Println(longestAlternatingSubarray([]int{1,2}, 2)) // 1
// 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.
fmt.Println(longestAlternatingSubarray([]int{2,3,4,5}, 4)) // 3
fmt.Println(longestAlternatingSubarray([]int{1,2,3,4,5,6,7,8,9}, 4)) // 3
fmt.Println(longestAlternatingSubarray([]int{9,8,7,6,5,4,3,2,1}, 4)) // 4
fmt.Println(longestAlternatingSubarray1([]int{3,2,5,4}, 5)) // 3
fmt.Println(longestAlternatingSubarray1([]int{1,2}, 2)) // 1
fmt.Println(longestAlternatingSubarray1([]int{2,3,4,5}, 4)) // 3
fmt.Println(longestAlternatingSubarray1([]int{1,2,3,4,5,6,7,8,9}, 4)) // 3
fmt.Println(longestAlternatingSubarray1([]int{9,8,7,6,5,4,3,2,1}, 4)) // 4
}