https://leetcode.com/contest/weekly-contest-71/problems/rabbits-in-forest/
挺有意思的题。找几个例子试一试就能过掉
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int, int> mp;
for (int i = 0; i < answers.size(); i++) {
mp[ answers[i] ] ++;
}
int ans = 0;
for (unordered_map<int, int>::iterator itr = mp.begin(); itr != mp.end(); itr++) {
int v = itr->second;
if (v<=itr->first+1) {
ans += itr->first + 1;
} else {
ans += (itr->first + 1)*(v/(itr->first+1));
if (v%(itr->first+1)) {
ans += (itr->first + 1);
}
}
}
return ans;
}
};