Leetcode #781 - Rabbits in Forest
There is a forest with an unknown number of rabbits. We asked n
rabbits:
βHow many rabbits have the same color as you?β
Each rabbit replied with a number, and we collected these answers in an integer array answers
, where answers[i]
is the response of the i-th
rabbit.
Your task is to determine the minimum number of rabbits that could be in the forest based on these replies.
π Example 1
Input:
answers = [1, 1, 2]
Output:
5
Explanation:
- The two rabbits that answered
1
could be the same color (e.g., red). - The rabbit that answered
2
cannot be red; otherwise, it would contradict the others. - Assume the rabbit that answered
2
is blue. Then there must be 2 more blue rabbits that didn't answer (since it said β2 others like meβ).
So the minimum number of rabbits is:
- 3 (those who answered) + 2 (additional blue rabbits) = 5
π Example 2
Input:
answers = [10, 10, 10]
Output:
11
Explanation:
- Each rabbit says there are 10 others like them β group size = 11.
- But with 3 rabbits giving the same answer, we can fit them into one group of 11 (with 8 unaccounted-for rabbits that didn't answer).
- So we assume one full group β 11 rabbits
β Constraints
1 <= answers.length <= 1000
0 <= answers[i] < 1000
π― Would you like to try it yourself? Click here to solve it on Leetcode!
π€ Curious how I did? [Check out my solution!] (https://leetcode.com/problems/rabbits-in-forest/solutions/6674788/leetcode-781-rabbits-in-forest-by-cpn3kq-qri3/)