# 781. Rabbits in Forest 森林中的兔子

@TOC

## # 题目描述

In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the same color as them. Those answers are placed in an array.

Return the minimum number of rabbits that could be in the forest.

Examples:

``````Input: answers = [1, 1, 2]
Output: 5
Explanation:
The two rabbits that answered "1" could both be the same color, say red.
The rabbit than answered "2" can't be red or the answers would be inconsistent.
Say the rabbit that answered "2" was blue.
Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.

Input: answers = [10, 10, 10]
Output: 11

Input: answers = []
Output: 0
``````

Note:

1. answers will have length at most 1000.
2. Each answers[i] will be an integer in the range [0, 999].

## # 解题方法

If `x+1` rabbits have same color, then we get `x+1` rabbits who all answer `x`. now `n` rabbits answer `x`. If `n%(x+1)==0`, we need `n/(x+1)` groups of `x+1` rabbits. If `n%(x+1)!=0`, we need `n/(x+1) + 1` groups of `x+1` rabbits. the number of groups is math.ceil(n/(x+1)) and it equals to (n+i)/(i+1) , which is more elegant.

``````class Solution(object):
"""
:rtype: int
"""
print count
return sum((count[x] + x) / (x + 1) * (x + 1) for x in count)
``````

C++代码如下：

``````class Solution {
public:
int numRabbits(vector<int>& answers) {
int res = 0;
unordered_map<int, int> m;
for (int a : answers) m[a]++;
for (auto a : m) {
res += (a.second + a.first) / (a.first + 1) * (a.first + 1);
}
return res;
}
};
``````

## # 日期

2018 年 3 月 6 日 2018 年 12 月 18 日 —— 改革开放40周年