781. Rabbits in Forest
题目描述
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.
|
|
Note:
- answers will have length at most 1000.
- Each answers[i] will be an integer in the range [0, 999].
题目大意
每个兔子都有一些颜色, answers
代表兔子告诉你有多少只兔子和自己颜色相同。求总共有多少只兔子。
解题思路
利用字典储存相同颜色兔子的群落,还可以容纳多少只兔子。
若可以容纳兔子大于0
,则群落数- 1
否则令ans
,重新计算群落数。
代码
|
|