Skip to content

Commit 3a5ba35

Browse files
committed
Add solution and test-cases for problem 2537
1 parent a01e89c commit 3a5ba35

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [2537.Count the Number of Good Subarrays][title]
2+
3+
## Description
4+
Given an integer array `nums` and an integer `k`, return the number of **good** subarrays of `nums`.
5+
6+
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]`.
7+
8+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [1,1,1,1,1], k = 10
14+
Output: 1
15+
Explanation: The only good subarray is the array nums itself.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: nums = [3,1,4,3,2,2,4], k = 2
22+
Output: 4
23+
Explanation: There are 4 different good subarrays:
24+
- [3,1,4,3,2,2] that has 2 pairs.
25+
- [3,1,4,3,2,2,4] that has 3 pairs.
26+
- [1,4,3,2,2,4] that has 2 pairs.
27+
- [4,3,2,2,4] that has 2 pairs.
28+
```
29+
30+
## 结语
31+
32+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
33+
34+
[title]: https://leetcode.com/problems/count-the-number-of-good-subarrays
35+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int64 {
4+
l := len(nums)
5+
count := make(map[int]int64)
6+
start, end := 0, 0
7+
8+
var (
9+
ans, pairs int64
10+
)
11+
12+
i64k := int64(k)
13+
for ; end < l; end++ {
14+
pairs += count[nums[end]]
15+
count[nums[end]]++
16+
17+
for ; start < end && pairs >= i64k; start++ {
18+
ans += int64(l - end)
19+
count[nums[start]]--
20+
pairs -= count[nums[start]]
21+
}
22+
}
23+
return ans
524
}

leetcode/2501-2600/2537.Count-the-Number-of-Good-Subarrays/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 1, 1, 1, 1}, 10, 1},
18+
{"TestCase2", []int{3, 1, 4, 3, 2, 2, 4}, 2, 4},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)