Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d1377a4

Browse files
author
scuyjzh
committed
add solutions to problem - "1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit"
1 parent d15f1da commit d1377a4

File tree

2 files changed

+86
-5
lines changed
  • src/com/scuyjzh/leetcode/medium/No_1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit

2 files changed

+86
-5
lines changed

‎README.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@
196196

197197
#### 滑动窗口 4:使用数据结构维护窗口性质
198198

199-
| # | 题目 | 题解 | 难度 | 标签 |
200-
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ------------------------------------------ |
201-
| 239 | [滑动窗口最大值](https://leetcode-cn.com/problems/sliding-window-maximum/) | [Java](./src/com/scuyjzh/leetcode/hard/No_0239_Sliding_Window_Maximum/Solution.java) | 困难 | 队列 数组 滑动窗口 单调队列 堆(优先队列) |
202-
| 480 | [滑动窗口中位数](https://leetcode-cn.com/problems/sliding-window-median/) | [Java](./src/com/scuyjzh/leetcode/hard/No_0480_Sliding_Window_Median/Solution.java) | 困难 | 数组 哈希表 滑动窗口 堆(优先队列) |
203-
| 220 | [存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0220_Contains_Duplicate_III/Solution.java) | 中等 | 数组 桶排序 有序集合 排序 滑动窗口 |
199+
| # | 题目 | 题解 | 难度 | 标签 |
200+
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------------------- |
201+
| 239 | [滑动窗口最大值](https://leetcode-cn.com/problems/sliding-window-maximum/) | [Java](./src/com/scuyjzh/leetcode/hard/No_0239_Sliding_Window_Maximum/Solution.java) | 困难 | 队列 数组 滑动窗口 单调队列 堆(优先队列) |
202+
| 480 | [滑动窗口中位数](https://leetcode-cn.com/problems/sliding-window-median/) | [Java](./src/com/scuyjzh/leetcode/hard/No_0480_Sliding_Window_Median/Solution.java) | 困难 | 数组 哈希表 滑动窗口 堆(优先队列) |
203+
| 220 | [存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0220_Contains_Duplicate_III/Solution.java) | 中等 | 数组 桶排序 有序集合 排序 滑动窗口 |
204+
| 1438 | [绝对差不超过限制的最长连续子数组](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [Java](./src/com/scuyjzh/leetcode/medium/No_1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit/Solution.java) | 中等 | 队列 数组 有序集合 滑动窗口 单调队列 堆(优先队列) |
204205

205206
### Array
206207

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.scuyjzh.leetcode.medium.No_1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 1438. 绝对差不超过限制的最长连续子数组
7+
*
8+
* 给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长
9+
* 连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者
10+
* 等于 limit 。
11+
*
12+
* 如果不存在满足条件的子数组,则返回 0 。
13+
*/
14+
class Solution {
15+
/**
16+
* 方法一:滑动窗口 + 有序集合
17+
*/
18+
public int longestSubarray1(int[] nums, int limit) {
19+
// 为了方便统计当前窗口内的最大值与最小值,使用平衡树来维护窗口内元素构成的有序集合
20+
TreeMap<Integer, Integer> map = new TreeMap<>();
21+
int n = nums.length;
22+
int left = 0, right = 0;
23+
int res = 0;
24+
// 枚举每一个位置作为右端点,找到其对应的最靠左的左端点,满足区间中最大值与最小值的差不超过 limit
25+
while (right < n) {
26+
map.put(nums[right], map.getOrDefault(nums[right], 0) + 1);
27+
while (map.lastKey() - map.firstKey() > limit) {
28+
map.put(nums[left], map.get(nums[left]) - 1);
29+
if (map.get(nums[left]) == 0) {
30+
map.remove(nums[left]);
31+
}
32+
left++;
33+
}
34+
res = Math.max(res, right - left + 1);
35+
right++;
36+
}
37+
return res;
38+
}
39+
40+
/**
41+
* 方法二:滑动窗口 + 单调队列
42+
*/
43+
public int longestSubarray2(int[] nums, int limit) {
44+
// 由于仅需要统计当前窗口内的最大值与最小值,因此也可以分别使用两个单调队列解决本题
45+
// 使用一个单调递增的队列 queMin 维护最小值,一个单调递减的队列 queMax 维护最大值
46+
// 这样只需要计算两个队列的队首的差值,即可知道当前窗口是否满足条件
47+
Deque<Integer> queMin = new ArrayDeque<>();
48+
Deque<Integer> queMax = new ArrayDeque<>();
49+
int n = nums.length;
50+
int left = 0, right = 0;
51+
int res = 0;
52+
while (right < n) {
53+
while (!queMax.isEmpty() && queMax.peekLast() < nums[right]) {
54+
queMax.pollLast();
55+
}
56+
while (!queMin.isEmpty() && queMin.peekLast() > nums[right]) {
57+
queMin.pollLast();
58+
}
59+
queMax.offerLast(nums[right]);
60+
queMin.offerLast(nums[right]);
61+
while (!queMax.isEmpty() && !queMin.isEmpty() && queMax.peekFirst() - queMin.peekFirst() > limit) {
62+
if (nums[left] == queMin.peekFirst()) {
63+
queMin.pollFirst();
64+
}
65+
if (nums[left] == queMax.peekFirst()) {
66+
queMax.pollFirst();
67+
}
68+
left++;
69+
}
70+
res = Math.max(res, right - left + 1);
71+
right++;
72+
}
73+
return res;
74+
}
75+
76+
public static void main(String[] args) {
77+
System.out.println(new Solution().longestSubarray1(new int[]{8, 2, 4, 7}, 4));
78+
System.out.println(new Solution().longestSubarray2(new int[]{10, 1, 2, 4, 7, 2}, 5));
79+
}
80+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /