Skip to content

Commit 82b4bea

Browse files
authored
Merge pull request #900 from 0xff-dev/3005
Add solution and test-cases for problem 3005
2 parents 206dc0b + ea0e25b commit 82b4bea

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [3005.Count Elements With Maximum 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 array `nums` consisting of **positive** integers.
5+
6+
Return the **total frequencies** of elements in `nums` such that those elements all have the **maximum** frequency.
7+
8+
The **frequency** of an element is the number of occurrences of that element in the array.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,2,2,3,1,4]
14+
Output: 4
15+
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
16+
So the number of elements in the array with maximum frequency is 4.
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Elements With Maximum Frequency
23-
```go
2421
```
25-
22+
Input: nums = [1,2,3,4,5]
23+
Output: 5
24+
Explanation: All elements of the array have a frequency of 1 which is the maximum.
25+
So the number of elements in the array with maximum frequency is 5.
26+
```
2627

2728
## 结语
2829

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) int {
4+
count := make([]int, 101)
5+
for _, n := range nums {
6+
count[n]++
7+
}
8+
9+
ans := 0
10+
freq := -1
11+
for _, c := range count {
12+
if c == 0 {
13+
continue
14+
}
15+
if c > freq {
16+
ans = 0
17+
freq = c
18+
}
19+
if c == freq {
20+
ans += freq
21+
}
22+
}
23+
return ans
524
}

leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 2, 3, 1, 4}, 4},
17+
{"TestCase2", []int{1, 2, 3, 4, 5}, 5},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)