思路:贪心
具体思路:将answer[i]相等的兔子放在一类(同一种颜色的兔子回答肯定一样),尽量让这些兔子使用的颜色数量最少,某颜色的兔子有x个,回答的是y个兔子跟自己一样。则x/(y+1)向上取整为组数z,z*(y+1)为最少的个数,把所有的类加起来即可。
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int, int> hash;
int ans = 0;
for (const auto& a : answers) {
hash[a]++;
}
for (auto& each : hash) {
int fi = each.first, se = each.second;
ans += ceil((double)se / (fi + 1)) * (fi + 1);
}
return ans;
}
};