-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc1438.java
More file actions
30 lines (28 loc) · 842 Bytes
/
Lc1438.java
File metadata and controls
30 lines (28 loc) · 842 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
28
29
30
package leetcode;
import java.util.TreeMap;
/**
* @author Kuma
* @date 2021年2月21日
* 1438. 绝对差不超过限制的最长连续子数组
*/
public class Lc1438 {
public int longestSubarray(int[] nums, int limit) {
int left = 0;
int right = 0;
TreeMap<Integer,Integer> map = new TreeMap<>();
int res = 0;
while (right < nums.length){
map.put(nums[right], map.getOrDefault(nums[right], 0) + 1);
while (map.lastKey() - map.firstKey() > limit){
map.put(nums[left], map.get(nums[left]) - 1);
if (map.get(nums[left]) == 0){
map.remove(nums[left]);
}
left++;
}
res = Math.max(res, right - left + 1);
right++;
}
return res;
}
}