-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2765-LongestAlternatingSubarray.go
More file actions
102 lines (89 loc) · 3.07 KB
/
2765-LongestAlternatingSubarray.go
File metadata and controls
102 lines (89 loc) · 3.07 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
package main
// 2765. Longest Alternating Subarray
// You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if:
// m is greater than 1.
// s1 = s0 + 1.
// The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2].
// In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m.
// Return the maximum length of all alternating subarrays present in nums or -1 if no such subarray exists.
// A subarray is a contiguous non-empty sequence of elements within an array.
// Example 1:
// Input: nums = [2,3,4,3,4]
// Output: 4
// Explanation: The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
// Example 2:
// Input: nums = [4,5,6]
// Output: 2
// Explanation: [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
// Constraints:
// 2 <= nums.length <= 100
// 1 <= nums[i] <= 10^4
import "fmt"
func alternatingSubarray(nums []int) int {
isAltSub := func (nums []int) bool {
a, b := nums[0], nums[1]
if b != a + 1 { return false }
for i := 2; i < len(nums); i++ {
if i % 2 == 0 && nums[i] != a { return false }
if i % 2 != 0 && nums[i] != b { return false }
}
return true
}
for i := len(nums); i >= 2; i-- {
for j := 0; j < len(nums) - i + 1; j++ {
if isAltSub(nums[j:j + i]) { return i }
}
}
return -1
}
func alternatingSubarray1(nums []int) int {
res,n := 0, len(nums)
max := func (a, b int) int { if b < a { return a; }; return b; }
for i := 0; i < n - 1; i++ {
t := 0
for j := i + 1; j < n; j++ {
if t % 2 == 0 && nums[j] - nums[j-1] == 1{
t++
} else if t % 2 == 1 && nums[j] - nums[j-1] == -1{
t++
} else{
break
}
}
res = max(res,t)
}
if res == 0 {
return -1
} else {
return res + 1
}
}
func alternatingSubarray2(nums []int) int {
n, i, res := len(nums), 1, -1
max := func (a, b int) int { if b < a { return a; }; return b; }
for i < n {
len := 1
k := 1
for i < n && nums[i] - nums[i-1] == k {
len ++
k = -k
i ++
}
if len != 1 {
res = max(res,len)
} else {
i ++
}
}
return res
}
func main() {
// Explanation: The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
fmt.Println(alternatingSubarray([]int{2,3,4,3,4})) // 4
// Explanation: [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
fmt.Println(alternatingSubarray([]int{4,5,6})) // 2
fmt.Println(alternatingSubarray1([]int{2,3,4,3,4})) // 4
fmt.Println(alternatingSubarray1([]int{4,5,6})) // 2
fmt.Println(alternatingSubarray2([]int{2,3,4,3,4})) // 4
fmt.Println(alternatingSubarray2([]int{4,5,6})) // 2
}