diff --git a/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/README.md b/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/README.md index e74fde81..ff8d602c 100755 --- a/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/README.md +++ b/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/README.md @@ -1,28 +1,29 @@ # [3005.Count Elements With Maximum Frequency][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an array `nums` consisting of **positive** integers. + +Return the **total frequencies** of elements in `nums` such that those elements all have the **maximum** frequency. + +The **frequency** of an element is the number of occurrences of that element in the array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,2,3,1,4] +Output: 4 +Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array. +So the number of elements in the array with maximum frequency is 4. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Count Elements With Maximum Frequency -```go ``` - +Input: nums = [1,2,3,4,5] +Output: 5 +Explanation: All elements of the array have a frequency of 1 which is the maximum. +So the number of elements in the array with maximum frequency is 5. +``` ## 结语 diff --git a/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution.go b/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution.go index d115ccf5..d5404a6f 100644 --- a/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution.go +++ b/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + count := make([]int, 101) + for _, n := range nums { + count[n]++ + } + + ans := 0 + freq := -1 + for _, c := range count { + if c == 0 { + continue + } + if c > freq { + ans = 0 + freq = c + } + if c == freq { + ans += freq + } + } + return ans } diff --git a/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution_test.go b/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution_test.go index 14ff50eb..d88538ed 100644 --- a/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution_test.go +++ b/leetcode/3001-3100/3005.Count-Elements-With-Maximum-Frequency/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 2, 3, 1, 4}, 4}, + {"TestCase2", []int{1, 2, 3, 4, 5}, 5}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }