Skip to content

Commit 13b8449

Browse files
authored
Merge pull request #826 from 0xff-dev/2962
Add solution and test-cases for problem 2962
2 parents ab66aa7 + 8f36743 commit 13b8449

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/2901-3000/2962.Count-Subarrays-Where-Max-Element-Appears-at-Least-K-Times/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [2962.Count Subarrays Where Max Element Appears at Least K Times][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums` and a **positive** integer `k`.
5+
6+
Return the number of subarrays where the **maximum** element of `nums` appears **at least** `k` times in that subarray.
7+
8+
A **subarray** is a contiguous sequence of elements within an array.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,3,2,3,3], k = 2
14+
Output: 6
15+
Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Subarrays Where Max Element Appears at Least K Times
23-
```go
2420
```
25-
21+
Input: nums = [1,4,2,1], k = 3
22+
Output: 0
23+
Explanation: No subarray contains the element 4 at least 3 times.
24+
```
2625

2726
## 结语
2827

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int64 {
4+
ans := int64(0)
5+
m := nums[0]
6+
for i := 1; i < len(nums); i++ {
7+
if nums[i] > m {
8+
m = nums[i]
9+
}
10+
}
11+
indies := make([]int, 0)
12+
for i := 0; i < len(nums); i++ {
13+
if nums[i] == m {
14+
indies = append(indies, i)
15+
}
16+
}
17+
ll := len(nums)
18+
if ll < k {
19+
return ans
20+
}
21+
le := 0
22+
for s, e := 0, k-1; e < len(indies); s, e = s+1, e+1 {
23+
left := int64(indies[s] - le)
24+
right := int64(ll - 1 - indies[e])
25+
ans += left + right + left*right + 1
26+
le = indies[s] + 1
27+
}
28+
return ans
529
}

leetcode/2901-3000/2962.Count-Subarrays-Where-Max-Element-Appears-at-Least-K-Times/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+
inputs []int
14+
k int
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 3, 2, 3, 3}, 2, 6},
18+
{"TestCase2", []int{1, 4, 2, 1}, 3, 0},
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.inputs, 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.inputs, 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)