From 3e9c33ba59de85338faeb14befa2b2d30bd18f5a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 14 Dec 2024 20:09:58 +0800 Subject: [PATCH] Add solution and test-cases for problem 2762 --- .../2762.Continuous-Subarrays/README.md | 43 ++++++++ .../2762.Continuous-Subarrays/Solution.go | 97 ++++++++++++++++++- .../Solution_test.go | 13 ++- 3 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 leetcode/2701-2800/2762.Continuous-Subarrays/README.md diff --git a/leetcode/2701-2800/2762.Continuous-Subarrays/README.md b/leetcode/2701-2800/2762.Continuous-Subarrays/README.md new file mode 100644 index 000000000..6ea636963 --- /dev/null +++ b/leetcode/2701-2800/2762.Continuous-Subarrays/README.md @@ -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 diff --git a/leetcode/2701-2800/2762.Continuous-Subarrays/Solution.go b/leetcode/2701-2800/2762.Continuous-Subarrays/Solution.go index d115ccf5e..b95ba09b4 100755 --- a/leetcode/2701-2800/2762.Continuous-Subarrays/Solution.go +++ b/leetcode/2701-2800/2762.Continuous-Subarrays/Solution.go @@ -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 } diff --git a/leetcode/2701-2800/2762.Continuous-Subarrays/Solution_test.go b/leetcode/2701-2800/2762.Continuous-Subarrays/Solution_test.go index 14ff50eb4..cf48a396e 100755 --- a/leetcode/2701-2800/2762.Continuous-Subarrays/Solution_test.go +++ b/leetcode/2701-2800/2762.Continuous-Subarrays/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }