Skip to main content
LeetCode 781, Medium. Topics: Array, Hash Table, Math, Greedy. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 47 parametrized pytest cases, and a playground notebook:

Problem

There is a forest with an unknown number of rabbits. We asked n rabbits “How many other rabbits have the same color as you?” and collected the answers in an integer array answers where answers[i] is the answer of the i<sup>th</sup> rabbit. Given the array answers, return the minimum number of rabbits that could be in the forest.

Examples

Constraints

  • 1 <= answers.length <= 1000
  • 0 <= answers[i] < 1000

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026