-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1438.go
125 lines (107 loc) · 2.99 KB
/
1438.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Source: https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit
// Title: Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
//
// Example 1:
//
// Input: nums = [8,2,4,7], limit = 4
// Output: 2
// Explanation:
// All subarrays are:
// - [8] with maximum absolute diff |8-8| = 0 <= 4.
// - [8,2] with maximum absolute diff |8-2| = 6 > 4.
// - [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
// - [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
// - [2] with maximum absolute diff |2-2| = 0 <= 4.
// - [2,4] with maximum absolute diff |2-4| = 2 <= 4.
// - [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
// - [4] with maximum absolute diff |4-4| = 0 <= 4.
// - [4,7] with maximum absolute diff |4-7| = 3 <= 4.
// - [7] with maximum absolute diff |7-7| = 0 <= 4.
// Therefore, the size of the longest subarray is 2.
//
// Example 2:
//
// Input: nums = [10,1,2,4,7,2], limit = 5
// Output: 4
// Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
//
// Example 3:
//
// Input: nums = [4,2,2,2,4,4,2,2], limit = 0
// Output: 3
//
// Constraints:
//
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^9
// 0 <= limit <= 10^9
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
type queue struct {
data []int
lIdx int
rIdx int
}
func newQueue(size int) *queue {
return &queue{
data: make([]int, size),
lIdx: 0,
rIdx: -1,
}
}
func (q queue) empty() bool {
return q.lIdx > q.rIdx
}
func (q queue) peekLeft() int {
return q.data[q.lIdx]
}
func (q queue) peekRight() int {
return q.data[q.rIdx]
}
func (q *queue) popLeft() {
q.lIdx++
}
func (q *queue) popRight() {
q.rIdx--
}
// func (q *queue) pushLeft(val int) {
// q.lIdx--
// q.data[q.lIdx] = val
// }
func (q *queue) pushRight(val int) {
q.rIdx++
q.data[q.rIdx] = val
}
func longestSubarray(nums []int, limit int) int {
n := len(nums)
decQ := newQueue(n)
incQ := newQueue(n)
ans := 0
lIdx := 0
for rIdx, rVal := range nums {
for !decQ.empty() && decQ.peekRight() < rVal {
decQ.popRight()
}
decQ.pushRight(rVal)
for !incQ.empty() && incQ.peekRight() > rVal {
incQ.popRight()
}
incQ.pushRight(rVal)
for decQ.peekLeft()-incQ.peekLeft() > limit {
if decQ.peekLeft() == nums[lIdx] {
decQ.popLeft()
}
if incQ.peekLeft() == nums[lIdx] {
incQ.popLeft()
}
lIdx++
}
ans = max(ans, rIdx-lIdx+1)
}
return ans
}