LeetCode 1742. Maximum Number of Balls in a Box

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int countBalls(int lowLimit, int highLimit) {
Map<Integer, Integer> m = new HashMap<>();
for (int i = lowLimit; i <= highLimit; ++i) {
int num = i, box = 0;
while (num > 0) {
box += num % 10;
num /= 10;
}
m.put(box, m.getOrDefault(box, 0) + 1);
}
int ans = 0;
for (Map.Entry<Integer, Integer> e : m.entrySet()) ans = Math.max(ans, e.getValue());
return ans;
}
}