Skip to content

Commit fd62b9f

Browse files
committed
Add solution 1438
1 parent d12dde0 commit fd62b9f

25 files changed

+579
-306
lines changed

README.md

Lines changed: 220 additions & 215 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode
2+
3+
func longestSubarray(nums []int, limit int) int {
4+
minStack, maxStack, left, res := []int{}, []int{}, 0, 0
5+
for right, num := range nums {
6+
for len(minStack) > 0 && nums[minStack[len(minStack)-1]] > num {
7+
minStack = minStack[:len(minStack)-1]
8+
}
9+
minStack = append(minStack, right)
10+
for len(maxStack) > 0 && nums[maxStack[len(maxStack)-1]] < num {
11+
maxStack = maxStack[:len(maxStack)-1]
12+
}
13+
maxStack = append(maxStack, right)
14+
if len(minStack) > 0 && len(maxStack) > 0 && nums[maxStack[0]]-nums[minStack[0]] > limit {
15+
if left == minStack[0] {
16+
minStack = minStack[1:]
17+
}
18+
if left == maxStack[0] {
19+
maxStack = maxStack[1:]
20+
}
21+
left++
22+
}
23+
if right-left+1 > res {
24+
res = right - left + 1
25+
}
26+
}
27+
return res
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1438 struct {
9+
para1438
10+
ans1438
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1438 struct {
16+
nums []int
17+
limit int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1438 struct {
23+
one int
24+
}
25+
26+
func Test_Problem1438(t *testing.T) {
27+
28+
qs := []question1438{
29+
30+
{
31+
para1438{[]int{8, 2, 4, 7}, 4},
32+
ans1438{2},
33+
},
34+
35+
{
36+
para1438{[]int{10, 1, 2, 4, 7, 2}, 5},
37+
ans1438{4},
38+
},
39+
40+
{
41+
para1438{[]int{4, 2, 2, 2, 4, 4, 2, 2}, 0},
42+
ans1438{3},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1438------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1438, q.para1438
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, longestSubarray(p.nums, p.limit))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/)
2+
3+
4+
## 题目
5+
6+
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`*.*
7+
8+
**Example 1:**
9+
10+
```
11+
Input: nums = [8,2,4,7], limit = 4
12+
Output: 2
13+
Explanation: All subarrays are:
14+
[8] with maximum absolute diff |8-8| = 0 <= 4.
15+
[8,2] with maximum absolute diff |8-2| = 6 > 4.
16+
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
17+
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
18+
[2] with maximum absolute diff |2-2| = 0 <= 4.
19+
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
20+
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
21+
[4] with maximum absolute diff |4-4| = 0 <= 4.
22+
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
23+
[7] with maximum absolute diff |7-7| = 0 <= 4.
24+
Therefore, the size of the longest subarray is 2.
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: nums = [10,1,2,4,7,2], limit = 5
31+
Output: 4
32+
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
33+
```
34+
35+
**Example 3:**
36+
37+
```
38+
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
39+
Output: 3
40+
```
41+
42+
**Constraints:**
43+
44+
- `1 <= nums.length <= 10^5`
45+
- `1 <= nums[i] <= 10^9`
46+
- `0 <= limit <= 10^9`
47+
48+
## 题目大意
49+
50+
给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit 。如果不存在满足条件的子数组,则返回 0 。
51+
52+
## 解题思路
53+
54+
- 最开始想到的思路是利用滑动窗口遍历一遍数组,每个窗口内排序,取出最大最小值。滑动窗口遍历一次的时间复杂度是 O(n),所以此题时间复杂度是否高效落在了排序算法上了。由于前后 2 个窗口数据是有关联的,仅仅只变动了 2 个数据(左窗口移出的数据和右窗口移进的数据),所以排序没有必要每次都重新排序。这里利用二叉排序树来排序,添加和删除元素时间复杂度是 O(log n),这种方法总的时间复杂度是 O(n log n)。空间复杂度 O(n)。
55+
- 二叉排序树的思路是否还有再优化的空间?答案是有。二叉排序树内维护了所有结点的有序关系,但是这个关系是多余的。此题只需要找到最大值和最小值,并不需要除此以外节点的有序信息。所以用二叉排序树是大材小用了。可以换成 2 个单调队列,一个维护窗口内的最大值,另一个维护窗口内的最小值。这样优化以后,时间复杂度降低到 O(n),空间复杂度 O(n)。具体实现见代码。
56+
- 单调栈的题还有第 42 题,第 84 题,第 496 题,第 503 题,第 739 题,第 856 题,第 901 题,第 907 题,第 1130 题,第 1425 题,第 1673 题。
57+
58+
## 代码
59+
60+
```go
61+
package leetcode
62+
63+
func longestSubarray(nums []int, limit int) int {
64+
minStack, maxStack, left, res := []int{}, []int{}, 0, 0
65+
for right, num := range nums {
66+
for len(minStack) > 0 && nums[minStack[len(minStack)-1]] > num {
67+
minStack = minStack[:len(minStack)-1]
68+
}
69+
minStack = append(minStack, right)
70+
for len(maxStack) > 0 && nums[maxStack[len(maxStack)-1]] < num {
71+
maxStack = maxStack[:len(maxStack)-1]
72+
}
73+
maxStack = append(maxStack, right)
74+
if len(minStack) > 0 && len(maxStack) > 0 && nums[maxStack[0]]-nums[minStack[0]] > limit {
75+
if left == minStack[0] {
76+
minStack = minStack[1:]
77+
}
78+
if left == maxStack[0] {
79+
maxStack = maxStack[1:]
80+
}
81+
left++
82+
}
83+
if right-left+1 > res {
84+
res = right - left + 1
85+
}
86+
}
87+
return res
88+
}
89+
```

website/content/ChapterFour/0900~0999/0901.Online-Stock-Span.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ Note that (for example) S.next(75) returned 4, because the last 4 prices
5757
单调栈类似的题
5858

5959
496. Next Greater Element I
60-
497. Next Greater Element II
61-
498. Daily Temperatures
62-
499. Sum of Subarray Minimums
63-
500. Largest Rectangle in Histogram
60+
503. Next Greater Element II
61+
739. Daily Temperatures
62+
907. Sum of Subarray Minimums
63+
84. Largest Rectangle in Histogram
6464

6565
## 代码
6666

website/content/ChapterFour/1400~1499/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ func kLengthApart(nums []int, k int) bool {
7676
----------------------------------------------
7777
<div style="display: flex;justify-content: space-between;align-items: center;">
7878
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1423.Maximum-Points-You-Can-Obtain-from-Cards/">⬅️上一页</a></p>
79-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/">下一页➡️</a></p>
79+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/">下一页➡️</a></p>
8080
</div>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/)
2+
3+
4+
## 题目
5+
6+
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`*.*
7+
8+
**Example 1:**
9+
10+
```
11+
Input: nums = [8,2,4,7], limit = 4
12+
Output: 2
13+
Explanation: All subarrays are:
14+
[8] with maximum absolute diff |8-8| = 0 <= 4.
15+
[8,2] with maximum absolute diff |8-2| = 6 > 4.
16+
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
17+
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
18+
[2] with maximum absolute diff |2-2| = 0 <= 4.
19+
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
20+
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
21+
[4] with maximum absolute diff |4-4| = 0 <= 4.
22+
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
23+
[7] with maximum absolute diff |7-7| = 0 <= 4.
24+
Therefore, the size of the longest subarray is 2.
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: nums = [10,1,2,4,7,2], limit = 5
31+
Output: 4
32+
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
33+
```
34+
35+
**Example 3:**
36+
37+
```
38+
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
39+
Output: 3
40+
```
41+
42+
**Constraints:**
43+
44+
- `1 <= nums.length <= 10^5`
45+
- `1 <= nums[i] <= 10^9`
46+
- `0 <= limit <= 10^9`
47+
48+
## 题目大意
49+
50+
给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit 。如果不存在满足条件的子数组,则返回 0 。
51+
52+
## 解题思路
53+
54+
- 最开始想到的思路是利用滑动窗口遍历一遍数组,每个窗口内排序,取出最大最小值。滑动窗口遍历一次的时间复杂度是 O(n),所以此题时间复杂度是否高效落在了排序算法上了。由于前后 2 个窗口数据是有关联的,仅仅只变动了 2 个数据(左窗口移出的数据和右窗口移进的数据),所以排序没有必要每次都重新排序。这里利用二叉排序树来排序,添加和删除元素时间复杂度是 O(log n),这种方法总的时间复杂度是 O(n log n)。空间复杂度 O(n)。
55+
- 二叉排序树的思路是否还有再优化的空间?答案是有。二叉排序树内维护了所有结点的有序关系,但是这个关系是多余的。此题只需要找到最大值和最小值,并不需要除此以外节点的有序信息。所以用二叉排序树是大材小用了。可以换成 2 个单调队列,一个维护窗口内的最大值,另一个维护窗口内的最小值。这样优化以后,时间复杂度降低到 O(n),空间复杂度 O(n)。具体实现见代码。
56+
- 单调栈的题还有第 42 题,第 84 题,第 496 题,第 503 题,第 739 题,第 856 题,第 901 题,第 907 题,第 1130 题,第 1425 题,第 1673 题。
57+
58+
## 代码
59+
60+
```go
61+
package leetcode
62+
63+
func longestSubarray(nums []int, limit int) int {
64+
minStack, maxStack, left, res := []int{}, []int{}, 0, 0
65+
for right, num := range nums {
66+
for len(minStack) > 0 && nums[minStack[len(minStack)-1]] > num {
67+
minStack = minStack[:len(minStack)-1]
68+
}
69+
minStack = append(minStack, right)
70+
for len(maxStack) > 0 && nums[maxStack[len(maxStack)-1]] < num {
71+
maxStack = maxStack[:len(maxStack)-1]
72+
}
73+
maxStack = append(maxStack, right)
74+
if len(minStack) > 0 && len(maxStack) > 0 && nums[maxStack[0]]-nums[minStack[0]] > limit {
75+
if left == minStack[0] {
76+
minStack = minStack[1:]
77+
}
78+
if left == maxStack[0] {
79+
maxStack = maxStack[1:]
80+
}
81+
left++
82+
}
83+
if right-left+1 > res {
84+
res = right - left + 1
85+
}
86+
}
87+
return res
88+
}
89+
```
90+
91+
92+
----------------------------------------------
93+
<div style="display: flex;justify-content: space-between;align-items: center;">
94+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/">⬅️上一页</a></p>
95+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/">下一页➡️</a></p>
96+
</div>

website/content/ChapterFour/1400~1499/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ func (pq *priorityQueue) Push(i interface{}) {
146146

147147
----------------------------------------------
148148
<div style="display: flex;justify-content: space-between;align-items: center;">
149-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/">⬅️上一页</a></p>
149+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/">⬅️上一页</a></p>
150150
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/">下一页➡️</a></p>
151151
</div>

0 commit comments

Comments
 (0)