1399: Count-Largest-Group
Easy
Simple brute-force all solutions with a hash-map solution.
You can just iterate through all the digits between
1
and n
and manually iterate through the
hash-map to find the number of groups with the
largest size.
Code:
class Solution {
public:
<int, int> mp;
unordered_mapint countLargestGroup(int n) {
for (int i = 1; i <= n; ++i) {
int copy = i;
int counter = 0;
while(copy > 0) {
+= (copy % 10);
counter /= 10;
copy }
++mp[counter];
}
int ans = 0, mx = 0;
for (auto &[key, value] : mp) {
if (value > mx) {
= 1;
ans = value;
mx } else if (value == mx) {
++ans;
}
}
return ans;
}
};