781. 森林中的兔子
题意:
给你一个answers数组,该数组存储每只兔子(知道有几个和它相同颜色的个数)。
问:
从题目的数组推出,至少有几只兔子。
思路:
北大大佬的,
博主的表达太垃圾了,借鉴大佬的。
AC
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int,int> ma;
for(auto x: answers)ma[x]++;
int res = 0;
for(auto x: ma){
int col = x.first;
int num = x.second;
if(num%(col+1) == 0) res += num;
else res += (num/(col+1)+1)*(col+1);
}
return res;
}
};