Skip to content

Commit 9eb928b

Browse files
authored
Merge pull request #1063 from 0xff-dev/2762
Add solution and test-cases for problem 2762
2 parents 8f2f9e8 + 3e9c33b commit 9eb928b

File tree

3 files changed

+144
-9
lines changed

3 files changed

+144
-9
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [2762.Continuous Subarrays][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums`. A subarray of `nums` is called continuous if:
5+
6+
- Let `i, i + 1, ..., j` be the indices in the subarray. Then, for each pair of indices `i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2`.
7+
8+
Return the total number of **continuous** subarrays.
9+
10+
A subarray is a contiguous **non-empty** sequence of elements within an array.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [5,4,2,4]
16+
Output: 8
17+
Explanation:
18+
Continuous subarray of size 1: [5], [4], [2], [4].
19+
Continuous subarray of size 2: [5,4], [4,2], [2,4].
20+
Continuous subarray of size 3: [4,2,4].
21+
Thereare no subarrys of size 4.
22+
Total continuous subarrays = 4 + 3 + 1 = 8.
23+
It can be shown that there are no more continuous subarrays.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: nums = [1,2,3]
30+
Output: 6
31+
Explanation:
32+
Continuous subarray of size 1: [1], [2], [3].
33+
Continuous subarray of size 2: [1,2], [2,3].
34+
Continuous subarray of size 3: [1,2,3].
35+
Total continuous subarrays = 3 + 2 + 1 = 6.
36+
```
37+
38+
## 结语
39+
40+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
41+
42+
[title]: https://leetcode.com/problems/continuous-subarrays
43+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,98 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type SegmentTreeNode2762 struct {
4+
Left, Right int
5+
Min, Max int
6+
LeftChild *SegmentTreeNode2762
7+
RightChild *SegmentTreeNode2762
8+
}
9+
10+
func buildSegmentTree2762(arr []int, left, right int) *SegmentTreeNode2762 {
11+
if left == right {
12+
return &SegmentTreeNode2762{
13+
Left: left,
14+
Right: right,
15+
Min: arr[left],
16+
Max: arr[left],
17+
}
18+
}
19+
20+
mid := (left + right) / 2
21+
leftChild := buildSegmentTree2762(arr, left, mid)
22+
rightChild := buildSegmentTree2762(arr, mid+1, right)
23+
24+
node := &SegmentTreeNode2762{
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 queryMin2762(node *SegmentTreeNode2762, 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 queryMin2762(node.LeftChild, left, right)
45+
} else if left > mid {
46+
return queryMin2762(node.RightChild, left, right)
47+
} else {
48+
leftMin := queryMin2762(node.LeftChild, left, mid)
49+
rightMin := queryMin2762(node.RightChild, mid+1, right)
50+
return min(leftMin, rightMin)
51+
}
52+
}
53+
54+
func queryMax2762(node *SegmentTreeNode2762, 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 queryMax2762(node.LeftChild, left, right)
63+
} else if left > mid {
64+
return queryMax2762(node.RightChild, left, right)
65+
} else {
66+
leftMax := queryMax2762(node.LeftChild, left, mid)
67+
rightMax := queryMax2762(node.RightChild, mid+1, right)
68+
return max(leftMax, rightMax)
69+
}
70+
}
71+
72+
func Solution(nums []int) int64 {
73+
l := len(nums)
74+
ans := int64(1)
75+
76+
tree := buildSegmentTree2762(nums, 0, l-1)
77+
start := 0
78+
var _max, _min int
79+
for end := 1; end < l; end++ {
80+
_max = queryMax2762(tree, start, end)
81+
_min = queryMin2762(tree, start, end)
82+
if _max-_min <= 2 {
83+
ans += int64(end-start) + 1
84+
continue
85+
}
86+
start++
87+
for ; start <= end; start++ {
88+
_max = queryMax2762(tree, start, end)
89+
_min = queryMin2762(tree, start, end)
90+
if _max-_min <= 2 {
91+
break
92+
}
93+
}
94+
ans += int64(end-start) + 1
95+
}
96+
97+
return ans
598
}

leetcode/2701-2800/2762.Continuous-Subarrays/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{5, 4, 2, 4}, 8},
17+
{"TestCase2", []int{1, 2, 3}, 6},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)