Rabbits in Forest
Real-world version: The Fewest Moths in the JarThe statement here is our own: the same inputs, outputs and technique.
Some of the moths in a jar were asked how many other moths share their own pattern, and replies holds the answers given, one per moth asked. Moths that were not asked said nothing.
A moth answering x belongs to a group of exactly x + 1 moths sharing one pattern, and moths in different groups never share a pattern.
Return the fewest moths the jar could hold.
Examples
Example 1
- Input
- replies = [1, 1, 1]
- Output
- 4
Two of the three moths can share a group of two, but the third needs a group of two of its own, so a fourth moth must be there unasked.
Example 2
- Input
- replies = [0, 1, 2, 3]
- Output
- 10
Each answer names a different group size, so the four groups hold one, two, three and four moths.
Example 3
- Input
- replies = [2, 2, 2, 2]
- Output
- 6
Groups of three are named. Three of the four moths fill one group and the fourth opens another, which needs two more moths to fill it.
Constraints
- 1 <= replies.length <= 1000
- 0 <= replies[i] < 1000
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def num_rabbits(replies: list[int]) -> int:public int numRabbits(int[] replies)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Smallest Missing Non-negative Integer After OperationsReal-world version: The Smallest Reading the Rack Cannot ReachMedium
- Missing NumberReal-world version: Unclaimed Badge NumberEasy
- Find Missing and Repeated ValuesReal-world version: Kiln Tray AuditEasy
- Number of Good PairsReal-world version: Matched Spoke PairsEasy