-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.go
80 lines (72 loc) · 1.82 KB
/
longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.go
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
package main
import (
"fmt"
)
func longestSubarray(nums []int, limit int) int {
maxDeque, minDeque := make([]int, 0, len(nums)), make([]int, 0, len(nums))
var left, a int
for right, n := range nums {
for len(maxDeque) != 0 && maxDeque[len(maxDeque)-1] < n {
maxDeque = maxDeque[:len(maxDeque)-1]
}
maxDeque = append(maxDeque, n)
for len(minDeque) != 0 && minDeque[len(minDeque)-1] > n {
minDeque = minDeque[:len(minDeque)-1]
}
minDeque = append(minDeque, n)
for maxDeque[0]-minDeque[0] > limit {
if maxDeque[0] == nums[left] {
maxDeque = maxDeque[1:]
}
if minDeque[0] == nums[left] {
minDeque = minDeque[1:]
}
left++
}
a = max(a, right-left+1)
}
return a
}
func main() {
testCases := []struct {
nums []int
limit int
want int
}{
{
nums: []int{24, 12, 71, 33, 5, 87, 10, 11, 3, 58, 2, 97, 97, 36, 32, 35, 15, 80, 24, 45, 38, 9, 22, 21, 33, 68, 22, 85, 35, 83, 92, 38, 59, 90, 42, 64, 61, 15, 4, 40, 50, 44, 54, 25, 34, 14, 33, 94, 66, 27, 78, 56, 3, 29, 3, 51, 19, 5, 93, 21, 58, 91, 65, 87, 55, 70, 29, 81, 89, 67, 58, 29, 68, 84, 4, 51, 87, 74, 42, 85, 81, 55, 8, 95, 39},
limit: 87,
want: 25,
},
{
nums: []int{8, 2, 4, 7},
limit: 4,
want: 2,
},
{
nums: []int{10, 1, 2, 4, 7, 2},
limit: 5,
want: 4,
},
{
nums: []int{4, 2, 2, 2, 4, 4, 2, 2},
limit: 0,
want: 3,
},
}
successes := 0
for _, tc := range testCases {
x := longestSubarray(tc.nums, tc.limit)
status := "ERROR"
if fmt.Sprint(x) == fmt.Sprint(tc.want) {
status = "OK"
successes++
}
fmt.Println(status, " Expected: ", tc.want, " Actual: ", x)
}
if l := len(testCases); successes == len(testCases) {
fmt.Printf("===\nSUCCESS: %d of %d tests ended successfully\n", successes, l)
} else {
fmt.Printf("===\nFAIL: %d tests failed\n", l-successes)
}
}