-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_1438.java
More file actions
27 lines (23 loc) · 893 Bytes
/
Copy pathleetCode_1438.java
File metadata and controls
27 lines (23 loc) · 893 Bytes
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
class Solution {
public int longestSubarray(int[] nums, int limit) {
int left = 0;
int res = 0;
int max = nums[left], min = nums[0];
List<Integer> ll = new ArrayList();
Deque<Integer> maxd = new ArrayDeque<>();
Deque<Integer> mind = new ArrayDeque<>();
int right=0;
for(; right < nums.length; right++){
while(maxd.size() > 0 && nums[right] > maxd.peekLast()) maxd.pollLast();
while(mind.size() > 0 && nums[right] < mind.peekLast()) mind.pollLast();
maxd.add(nums[right]);
mind.add(nums[right]);
if(maxd.peek() - mind.peek() > limit){
if(nums[left] == mind.peek()) mind.poll();
if(nums[left] == maxd.peek()) maxd.poll();
left++;
}
}
return right-left;
}
}