diff --git a/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/README.md b/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/README.md new file mode 100644 index 000000000..fc4b8221d --- /dev/null +++ b/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/README.md @@ -0,0 +1,35 @@ +# [2537.Count the Number of Good Subarrays][title] + +## Description +Given an integer array `nums` and an integer `k`, return the number of **good** subarrays of `nums`. + +A subarray `arr` is **good** if there are **at least** `k` pairs of indices `(i, j)` such that `i < j` and `arr[i] == arr[j]`. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +``` +Input: nums = [1,1,1,1,1], k = 10 +Output: 1 +Explanation: The only good subarray is the array nums itself. +``` + +**Example 2:** + +``` +Input: nums = [3,1,4,3,2,2,4], k = 2 +Output: 4 +Explanation: There are 4 different good subarrays: +- [3,1,4,3,2,2] that has 2 pairs. +- [3,1,4,3,2,2,4] that has 3 pairs. +- [1,4,3,2,2,4] that has 2 pairs. +- [4,3,2,2,4] that has 2 pairs. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/count-the-number-of-good-subarrays +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution.go b/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution.go index d115ccf5e..adb6c8c1d 100755 --- a/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution.go +++ b/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int64 { + l := len(nums) + count := make(map[int]int64) + start, end := 0, 0 + + var ( + ans, pairs int64 + ) + + i64k := int64(k) + for ; end < l; end++ { + pairs += count[nums[end]] + count[nums[end]]++ + + for ; start < end && pairs >= i64k; start++ { + ans += int64(l - end) + count[nums[start]]-- + pairs -= count[nums[start]] + } + } + return ans } diff --git a/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution_test.go b/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution_test.go index 14ff50eb4..69720b102 100755 --- a/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution_test.go +++ b/leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 1, 1, 1, 1}, 10, 1}, + {"TestCase2", []int{3, 1, 4, 3, 2, 2, 4}, 2, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }