Skip to content

Commit d537a9d

Browse files
authored
Create maximum-number-of-balls-in-a-box.cpp
1 parent 3310800 commit d537a9d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(nlogm)
2+
// Space: O(logm)
3+
4+
class Solution {
5+
public:
6+
int countBalls(int lowLimit, int highLimit) {
7+
unordered_map<int, int> count;
8+
for (int i = lowLimit; i <= highLimit; ++i) {
9+
int total = 0;
10+
for (int d = i; d; d /= 10) {
11+
total += d % 10;
12+
}
13+
++count[total];
14+
}
15+
return max_element(cbegin(count), cend(count),
16+
[](const auto& a, const auto& b) {
17+
return a.second < b.second;
18+
})->second;
19+
}
20+
};

0 commit comments

Comments
 (0)