Skip to content

Commit 64cb4fd

Browse files
authored
Merge pull request #824 from 0xff-dev/2958
Add solution and test-cases for problem 2958
2 parents d8ca883 + a8279f1 commit 64cb4fd

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

leetcode/2901-3000/2958.Length-of-Longest-Subarray-With-at-Most-K-Frequency/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [2958.Length of Longest Subarray With at Most K Frequency][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 an integer `k`.
5+
6+
The **frequency** of an element `x` is the number of times it occurs in an array.
7+
8+
An array is called **good** if the frequency of each element in this array is **less than or equal** to `k`.
9+
10+
Return the length of the **longest good** subarray of `nums`.
11+
12+
A **subarray** is a contiguous non-empty sequence of elements within an array.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,2,3,1,2,3,1,2], k = 2
18+
Output: 6
19+
Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
20+
It can be shown that there are no good subarrays with length more than 6.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Length of Longest Subarray With at Most K Frequency
23-
```go
2425
```
26+
Input: nums = [1,2,1,2,1,2,1,2], k = 1
27+
Output: 2
28+
Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
29+
It can be shown that there are no good subarrays with length more than 2.
30+
```
31+
32+
**Example 3:**
2533

34+
```
35+
Input: nums = [5,5,5,5,5,5,5], k = 4
36+
Output: 4
37+
Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
38+
It can be shown that there are no good subarrays with length more than 4.
39+
```
2640

2741
## 结语
2842

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
start, end := 0, 0
5+
ans := 1
6+
length := len(nums)
7+
count := make(map[int]int)
8+
for ; end < length; end++ {
9+
count[nums[end]]++
10+
if count[nums[end]] <= k {
11+
if r := end - start + 1; r > ans {
12+
ans = r
13+
}
14+
continue
15+
}
16+
for count[nums[end]] > k {
17+
start++
18+
count[nums[start-1]]--
19+
}
20+
}
21+
return ans
522
}

leetcode/2901-3000/2958.Length-of-Longest-Subarray-With-at-Most-K-Frequency/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ 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 int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3, 1, 2, 3, 1, 2}, 2, 6},
18+
{"TestCase2", []int{1, 2, 1, 2, 1, 2, 1, 2}, 1, 2},
19+
{"TestCase3", []int{5, 5, 5, 5, 5, 5, 5}, 4, 4},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)