Hello everyone , My name is Qi Guanjie （qí guān jié ）, stay GitHub & CSDN Record the process of learning , Time flies , Future period , come on. ~ Bloggers are only in GitHub & CSDN Chinese blog , The only blog update address is ： Qi Guanjie's blog , In the near future, we will gradually synchronize the relevant records to GitHub：Algorithmic-learning-records, Most of them are my records , If reprinted, please attach the original address , thank you .
Due to the need of study and work , Algorithm brush problem will gradually change from C++ towards Python3 excessive , In transition , If the implementation is not very beautiful , Please forgive me .
This article was originally written by Qi Guanjie , Please support the original , Some platforms have been stealing blog articles maliciously ！！！
If you need to contact the blogger , You can contact me on wechat ：qiguanjie2015
In the forest , Every rabbit has a color . Some of the rabbits （ It could be all ） Tell you how many other rabbits have the same color as themselves . We put these answers in
answers In the array .
Return to the minimum number of rabbits in the forest .
Example : Input : answers = [1, 1, 2] Output : 5 explain : Two answered "1" The rabbit of may have the same color , Set it to red . Then I answered "2" The rabbit is not red , Otherwise their answers will contradict each other . Let's answer "2" The rabbit is blue . Besides , There should be something else in the forest 2 Only the blue rabbit's answer is not included in the array . So the minimum number of rabbits in the forest is 5: 3 Only answers and 2 There is no answer . Input : answers = [10, 10, 10] Output : 11 Input : answers =  Output : 0
answersThe maximum length of is
[0, 999]Range of integers .
To the same , And within the same number of colors , Just merge .
class Solution: def numRabbits(self, answers: List[int]) -> int: ans = 0 answers = sorted(answers) i = 0 while i < len(answers): j = i+1 while j < len(answers) and answers[j] == answers[i] and j-i <= answers[i]: j += 1 ans += answers[i]+1 i = j return ans