Skip to content

Commit 1214d21

Browse files
committed
1224. 最大相等频率
1 parent 66c48eb commit 1214d21

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

leetcode/weekly/158/d/d.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import "sort"
4+
5+
// github.com/EndlessCheng/codeforces-go
6+
func maxEqualFreq(a []int) (ans int) {
7+
c := map[int]int{}
8+
cc := make([]int, len(a)+1)
9+
for i, v := range a {
10+
c[v]++
11+
c := c[v]
12+
cc[c]++
13+
if cc[c]*c == i {
14+
ans = i + 1
15+
} else if cc[c]*c == i+1 && i+1 < len(a) {
16+
ans = i + 2
17+
}
18+
}
19+
return
20+
}
21+
22+
func maxEqualFreq2(a []int) (ans int) {
23+
c := map[int]int{}
24+
cc := map[int]int{}
25+
for i, v := range a {
26+
if c := c[v]; cc[c] > 1 {
27+
cc[c]--
28+
} else if cc[c] == 1 {
29+
delete(cc, c)
30+
}
31+
c[v]++
32+
cc[c[v]]++
33+
if len(cc) > 2 {
34+
continue
35+
}
36+
cs := make([]int, 0, len(cc))
37+
for c := range cc {
38+
cs = append(cs, c)
39+
}
40+
sort.Ints(cs)
41+
if len(cc) == 1 {
42+
if cs[0] == 1 || cc[cs[0]] == 1 {
43+
ans = i + 1
44+
}
45+
} else {
46+
if cs[0] == 1 && cc[cs[0]] == 1 || cs[1]-cs[0] == 1 && cc[cs[1]] == 1 {
47+
ans = i + 1
48+
}
49+
}
50+
}
51+
return
52+
}

leetcode/weekly/158/d/d_test.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)