Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions leetcode/2701-2800/2762.Continuous-Subarrays/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# [2762.Continuous Subarrays][title]

## Description
You are given a **0-indexed** integer array `nums`. A subarray of `nums` is called continuous if:

- 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`.

Return the total number of **continuous** subarrays.

A subarray is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

```
Input: nums = [5,4,2,4]
Output: 8
Explanation:
Continuous subarray of size 1: [5], [4], [2], [4].
Continuous subarray of size 2: [5,4], [4,2], [2,4].
Continuous subarray of size 3: [4,2,4].
Thereare no subarrys of size 4.
Total continuous subarrays = 4 + 3 + 1 = 8.
It can be shown that there are no more continuous subarrays.
```

**Example 2:**

```
Input: nums = [1,2,3]
Output: 6
Explanation:
Continuous subarray of size 1: [1], [2], [3].
Continuous subarray of size 2: [1,2], [2,3].
Continuous subarray of size 3: [1,2,3].
Total continuous subarrays = 3 + 2 + 1 = 6.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/continuous-subarrays
[me]: https://github.com/kylesliu/awesome-golang-algorithm
97 changes: 95 additions & 2 deletions leetcode/2701-2800/2762.Continuous-Subarrays/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,98 @@
package Solution

func Solution(x bool) bool {
return x
type SegmentTreeNode2762 struct {
Left, Right int
Min, Max int
LeftChild *SegmentTreeNode2762
RightChild *SegmentTreeNode2762
}

func buildSegmentTree2762(arr []int, left, right int) *SegmentTreeNode2762 {
if left == right {
return &SegmentTreeNode2762{
Left: left,
Right: right,
Min: arr[left],
Max: arr[left],
}
}

mid := (left + right) / 2
leftChild := buildSegmentTree2762(arr, left, mid)
rightChild := buildSegmentTree2762(arr, mid+1, right)

node := &SegmentTreeNode2762{
Left: left,
Right: right,
Min: min(leftChild.Min, rightChild.Min),
Max: max(leftChild.Max, rightChild.Max),
LeftChild: leftChild,
RightChild: rightChild,
}

return node
}

func queryMin2762(node *SegmentTreeNode2762, left, right int) int {
if node.Left == left && node.Right == right {
return node.Min
}

mid := (node.Left + node.Right) / 2

if right <= mid {
return queryMin2762(node.LeftChild, left, right)
} else if left > mid {
return queryMin2762(node.RightChild, left, right)
} else {
leftMin := queryMin2762(node.LeftChild, left, mid)
rightMin := queryMin2762(node.RightChild, mid+1, right)
return min(leftMin, rightMin)
}
}

func queryMax2762(node *SegmentTreeNode2762, left, right int) int {
if node.Left == left && node.Right == right {
return node.Max
}

mid := (node.Left + node.Right) / 2

if right <= mid {
return queryMax2762(node.LeftChild, left, right)
} else if left > mid {
return queryMax2762(node.RightChild, left, right)
} else {
leftMax := queryMax2762(node.LeftChild, left, mid)
rightMax := queryMax2762(node.RightChild, mid+1, right)
return max(leftMax, rightMax)
}
}

func Solution(nums []int) int64 {
l := len(nums)
ans := int64(1)

tree := buildSegmentTree2762(nums, 0, l-1)
start := 0
var _max, _min int
for end := 1; end < l; end++ {
_max = queryMax2762(tree, start, end)
_min = queryMin2762(tree, start, end)
if _max-_min <= 2 {
ans += int64(end-start) + 1
continue
}
start++
for ; start <= end; start++ {
_max = queryMax2762(tree, start, end)
_min = queryMin2762(tree, start, end)
if _max-_min <= 2 {
break
}
}
ans += int64(end-start) + 1
}

return ans
}
13 changes: 6 additions & 7 deletions leetcode/2701-2800/2762.Continuous-Subarrays/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{5, 4, 2, 4}, 8},
{"TestCase2", []int{1, 2, 3}, 6},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading