Learnitweb

LeetCode 781 — Rabbits in Forest

Problem Summary

You are given an integer array answers, where each answers[i] represents:

A rabbit says:
“There are answers[i] other rabbits that have the same color as me.”

Your task is to compute the minimum number of rabbits that could be in the forest.

Example
Input: answers = [1, 1, 2]
Output: 5
Explanation:

  • Two rabbits say “1 other rabbit has my color” → together form a group of 2 rabbits.
  • One rabbit says “2 other rabbits have my color” → requires a group size of 3 rabbits.
    Minimum = 2 + 3 = 5.

Example
Input: answers = [10, 10, 10]
Output: 11
Explanation:
Each rabbit says there are 10 rabbits of its color; group size = 11.
We have 3 rabbits saying this, but they can all be in the same group of 11.

Constraints

  • 1 ≤ answers.length ≤ 1000
  • 0 ≤ answers[i] ≤ 1000

Key Insight

If a rabbit says “there are x other rabbits of my color”, then the size of its color group must be:

group size = x + 1

For example:

  • If a rabbit says 0 → group size = 1
  • If a rabbit says 1 → group size = 2
  • If a rabbit says 2 → group size = 3
    etc.

If several rabbits give the same number x, they can share the same group, but only up to the allowed group size.


Approach (Simple English)

  1. Count how many rabbits say each value x.
  2. For each unique x:
    • The group size is x + 1.
    • If count rabbits gave that answer:
      • They fill groups of size x + 1.
      • Number of groups needed = ceil(count / (x + 1))
    • Total rabbits contributed = number_of_groups * (x + 1)
  3. Sum results for all x.

Why This Works

Each answer x forces rabbits into buckets of size x + 1.
If more rabbits claim the same x than one group allows, they must form multiple such groups.


Java Code (Optimal Solution)

import java.util.*;

class Solution {
    public int numRabbits(int[] answers) {
        Map<Integer, Integer> freq = new HashMap<>();

        // Count occurrences
        for (int x : answers) {
            freq.put(x, freq.getOrDefault(x, 0) + 1);
        }

        int total = 0;

        // Process each unique answer
        for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
            int x = entry.getKey();
            int count = entry.getValue();

            int groupSize = x + 1;

            // number of groups needed
            int groups = (count + groupSize - 1) / groupSize;

            total += groups * groupSize;
        }

        return total;
    }
}

Dry Run

Input:

answers = [1, 1, 2]

Step 1: Count frequencies

  • 1 → 2 rabbits
  • 2 → 1 rabbit

Step 2: Process each x

For x = 1

  • group size = 2
  • count = 2
  • groups = ceil(2 / 2) = 1
  • rabbits = 1 * 2 = 2

For x = 2

  • group size = 3
  • count = 1
  • groups = ceil(1 / 3) = 1
  • rabbits = 1 * 3 = 3

Total = 2 + 3 = 5


Complexity Analysis

Time Complexity:
O(n), counting + iteration through map.

Space Complexity:
O(n), for storing frequencies.


Edge Cases

  • All rabbits say 0 → each is a unique color → answer = number of rabbits
  • All rabbits say the same x → they fill up groups of size x+1
  • Single rabbit → group size = 1
  • Large answers (like 1000) work normally since group sizes scale accordingly