Skip to content

Commit eb15e35

Browse files
committed
maxEqualFreq
1 parent 4e5df0d commit eb15e35

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

maximum-equal-frequency/export.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package index
2+
3+
func Max(a, b int) int {
4+
return max(a, b)
5+
}
6+
func MaxEqualFreq(nums []int) int {
7+
8+
return maxEqualFreq(nums)
9+
}

maximum-equal-frequency/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/masx200/leetcode-test/maximum-equal-frequency
2+
3+
go 1.19

maximum-equal-frequency/index.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package index
2+
3+
func maxEqualFreq(nums []int) (ans int) {
4+
freq := map[int]int{}
5+
count := map[int]int{}
6+
maxFreq := 0
7+
for i, num := range nums {
8+
if count[num] > 0 {
9+
freq[count[num]]--
10+
}
11+
count[num]++
12+
maxFreq = max(maxFreq, count[num])
13+
freq[count[num]]++
14+
if maxFreq == 1 ||
15+
freq[maxFreq]*maxFreq+freq[maxFreq-1]*(maxFreq-1) == i+1 && freq[maxFreq] == 1 ||
16+
freq[maxFreq]*maxFreq+1 == i+1 && freq[1] == 1 {
17+
ans = max(ans, i+1)
18+
}
19+
}
20+
return
21+
}
22+
23+
func max(a, b int) int {
24+
if b > a {
25+
return b
26+
}
27+
return a
28+
}

0 commit comments

Comments
 (0)