Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82be7a1

Browse files
author
Shuo
committed
A: Count Largest Group
1 parent 7af862d commit 82be7a1

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem1399
2+
3+
func countLargestGroup(n int) int {
4+
group := make(map[int]int)
5+
for i := 1; i <= n; i++ {
6+
s := 0
7+
for x := i; x > 0; x /= 10 {
8+
s += x % 10
9+
}
10+
group[s]++
11+
}
12+
ans, max := 0, 0
13+
for _, v := range group {
14+
if v == max {
15+
ans++
16+
} else if v > max {
17+
ans, max = 1, v
18+
}
19+
}
20+
return ans
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem1399
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in int
7+
want int
8+
}
9+
10+
func TestCountLargestGroup(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: 13,
14+
want: 4,
15+
},
16+
{
17+
in: 2,
18+
want: 2,
19+
},
20+
{
21+
in: 15,
22+
want: 6,
23+
},
24+
{
25+
in: 24,
26+
want: 5,
27+
},
28+
}
29+
for _, tt := range tests {
30+
got := countLargestGroup(tt.in)
31+
if got != tt.want {
32+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /