-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution1742.go
41 lines (36 loc) · 909 Bytes
/
solution1742.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package solution1742
// ============================================================================
// 1742. Maximum Number of Balls in a Box
// URL: https://leetcode.com/problems/maximum-number-of-balls-in-a-box/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1742---Maximum-Number-of-Balls-in-a-Box
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_countBalls-24 3749175 326.7 ns/op 336 B/op 3 allocs/op
PASS
*/
func countBalls(lowLimit int, highLimit int) int {
ans := 0
freq := make(map[int]int)
sum := func(num int) int {
res := 0
for num > 0 {
res += num % 10
num /= 10
}
return res
}
for i := lowLimit; i <= highLimit; i++ {
n := sum(i)
freq[n]++
}
for _, v := range freq {
if v > ans {
ans = v
}
}
return ans
}