Class Solution

java.lang.Object
g0701_0800.s0781_rabbits_in_forest.Solution

public class Solution extends Object
781 - Rabbits in Forest.<p>Medium</p> <p>There is a forest with an unknown number of rabbits. We asked n rabbits <strong>&ldquo;How many rabbits have the same color as you?&rdquo;</strong> and collected the answers in an integer array <code>answers</code> where <code>answers[i]</code> is the answer of the <code>i<sup>th</sup></code> rabbit.</p> <p>Given the array <code>answers</code>, return <em>the minimum number of rabbits that could be in the forest</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> answers = [1,1,2]</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong></p> <pre><code> The two rabbits that answered &quot;1&quot; could both be the same color, say red. The rabbit that answered &quot;2&quot; can't be red or the answers would be inconsistent. Say the rabbit that answered &quot;2&quot; 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. </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> answers = [10,10,10]</p> <p><strong>Output:</strong> 11</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= answers.length <= 1000</code></li> <li><code>0 <= answers[i] < 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numRabbits

      public int numRabbits(int[] answers)