Skip to content

Commit 68d1e1e

Browse files
authored
Added Maximum Number of Balls in a Box.java
1 parent bde0955 commit 68d1e1e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int countBalls(int lowLimit, int highLimit) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
int maxCount = 0;
5+
for (int num = lowLimit; num <= highLimit; num++) {
6+
int digitSum = getDigitSum(num);
7+
map.put(digitSum, map.getOrDefault(digitSum, 0) + 1);
8+
maxCount = Math.max(maxCount, map.get(digitSum));
9+
}
10+
return maxCount;
11+
}
12+
13+
private int getDigitSum(int num) {
14+
int sum = 0;
15+
while (num > 0) {
16+
sum += num % 10;
17+
num /= 10;
18+
}
19+
return sum;
20+
}
21+
}

0 commit comments

Comments
 (0)