forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1438.java
More file actions
21 lines (19 loc) · 702 Bytes
/
1438.java
File metadata and controls
21 lines (19 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int longestSubarray(int[] nums, int limit) {
Deque<Integer> maxq = new ArrayDeque();
Deque<Integer> minq = new ArrayDeque();
int i = 0;
for (int a : nums) {
while (maxq.size() > 0 && a > maxq.peekLast()) maxq.pollLast();
while (minq.size() > 0 && a < minq.peekLast()) minq.pollLast();
maxq.offer(a);
minq.offer(a);
if (maxq.getFirst() - minq.getFirst() > limit) {
if (maxq.getFirst() == nums[i]) maxq.pollFirst();
if (minq.getFirst() == nums[i]) minq.pollFirst();
i++;
}
}
return nums.length - i;
}
}