Skip to content

Commit 436b35e

Browse files
authored
Merge pull request #914 from 0xff-dev/1438
Add solution and test-cases for problem 1438
2 parents 082609c + 8c83bba commit 436b35e

File tree

3 files changed

+151
-12
lines changed

3 files changed

+151
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit][title]
2+
3+
## Description
4+
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`.
5+
6+
**Example 1:**
7+
8+
```
9+
Input: nums = [8,2,4,7], limit = 4
10+
Output: 2
11+
Explanation: All subarrays are:
12+
[8] with maximum absolute diff |8-8| = 0 <= 4.
13+
[8,2] with maximum absolute diff |8-2| = 6 > 4.
14+
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
15+
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
16+
[2] with maximum absolute diff |2-2| = 0 <= 4.
17+
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
18+
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
19+
[4] with maximum absolute diff |4-4| = 0 <= 4.
20+
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
21+
[7] with maximum absolute diff |7-7| = 0 <= 4.
22+
Therefore, the size of the longest subarray is 2.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: nums = [10,1,2,4,7,2], limit = 5
29+
Output: 4
30+
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
37+
Output: 3
38+
```
39+
40+
## 结语
41+
42+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
43+
44+
[title]: https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit
45+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,97 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type SegmentTreeNode1438 struct {
4+
Left, Right int
5+
Min, Max int
6+
LeftChild *SegmentTreeNode1438
7+
RightChild *SegmentTreeNode1438
8+
}
9+
10+
func buildSegmentTree1438(arr []int, left, right int) *SegmentTreeNode1438 {
11+
if left == right {
12+
return &SegmentTreeNode1438{
13+
Left: left,
14+
Right: right,
15+
Min: arr[left],
16+
Max: arr[left],
17+
}
18+
}
19+
20+
mid := (left + right) / 2
21+
leftChild := buildSegmentTree1438(arr, left, mid)
22+
rightChild := buildSegmentTree1438(arr, mid+1, right)
23+
24+
node := &SegmentTreeNode1438{
25+
Left: left,
26+
Right: right,
27+
Min: min(leftChild.Min, rightChild.Min),
28+
Max: max(leftChild.Max, rightChild.Max),
29+
LeftChild: leftChild,
30+
RightChild: rightChild,
31+
}
32+
33+
return node
34+
}
35+
36+
func queryMin1438(node *SegmentTreeNode1438, left, right int) int {
37+
if node.Left == left && node.Right == right {
38+
return node.Min
39+
}
40+
41+
mid := (node.Left + node.Right) / 2
42+
43+
if right <= mid {
44+
return queryMin1438(node.LeftChild, left, right)
45+
} else if left > mid {
46+
return queryMin1438(node.RightChild, left, right)
47+
} else {
48+
leftMin := queryMin1438(node.LeftChild, left, mid)
49+
rightMin := queryMin1438(node.RightChild, mid+1, right)
50+
return min(leftMin, rightMin)
51+
}
52+
}
53+
54+
func queryMax1438(node *SegmentTreeNode1438, left, right int) int {
55+
if node.Left == left && node.Right == right {
56+
return node.Max
57+
}
58+
59+
mid := (node.Left + node.Right) / 2
60+
61+
if right <= mid {
62+
return queryMax1438(node.LeftChild, left, right)
63+
} else if left > mid {
64+
return queryMax1438(node.RightChild, left, right)
65+
} else {
66+
leftMax := queryMax1438(node.LeftChild, left, mid)
67+
rightMax := queryMax1438(node.RightChild, mid+1, right)
68+
return max(leftMax, rightMax)
69+
}
70+
}
71+
72+
// 滑动窗口, 线段树?
73+
func Solution(nums []int, limit int) int {
74+
l := len(nums)
75+
tree := buildSegmentTree1438(nums, 0, l-1)
76+
start, end := 0, 0
77+
ans := 0
78+
79+
var a, b int
80+
for ; end < l; end++ {
81+
a = queryMax1438(tree, start, end)
82+
b = queryMin1438(tree, start, end)
83+
if a-b <= limit {
84+
ans = max(ans, end-start+1)
85+
continue
86+
}
87+
for start < end {
88+
start++
89+
a = queryMax1438(tree, start, end)
90+
b = queryMin1438(tree, start, end)
91+
if a-b <= limit {
92+
break
93+
}
94+
}
95+
}
96+
return ans
597
}

leetcode/1401-1500/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/Solution_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,32 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
limit int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{8, 2, 4, 7}, 4, 2},
18+
{"TestCase2", []int{10, 1, 2, 4, 7, 2}, 5, 4},
19+
{"TestCase3", []int{4, 2, 2, 2, 4, 4, 2, 2}, 0, 3},
20+
{"TestCase4", []int{24, 12, 71, 33, 5, 87, 10, 11, 3, 58, 2, 97, 97, 36, 32, 35, 15, 80, 24, 45, 38, 9, 22, 21, 33, 68, 22, 85, 35, 83, 92, 38, 59, 90, 42, 64, 61, 15, 4, 40, 50, 44, 54, 25, 34, 14, 33, 94, 66, 27, 78, 56, 3, 29, 3, 51, 19, 5, 93, 21, 58, 91, 65, 87, 55, 70, 29, 81, 89, 67, 58, 29, 68, 84, 4, 51, 87, 74, 42, 85, 81, 55, 8, 95, 39}, 87, 25},
1921
}
2022

2123
// 开始测试
2224
for i, c := range cases {
2325
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
26+
got := Solution(c.nums, c.limit)
2527
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
29+
c.expect, got, c.nums, c.limit)
2830
}
2931
})
3032
}
3133
}
3234

33-
// 压力测试
35+
// 压力测试
3436
func BenchmarkSolution(b *testing.B) {
3537
}
3638

37-
// 使用案列
39+
// 使用案列
3840
func ExampleSolution() {
3941
}

0 commit comments

Comments
 (0)