Egg Dropping Problem

Pattern: Partition and interval DP

hard

Turn the question round: with m drops and e eggs, how many floors can be settled? covered(m, e) = covered(m-1, e-1) + covered(m-1, e) + 1: the drop floor, the floors below if it breaks, and the floors above if not. Find the smallest m covering f floors.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

With e identical eggs and a building of f floors, find the fewest drops that are guaranteed to find the highest floor from which an egg survives. A broken egg cannot be reused.

Think first

  • What happens after a drop from floor x?
  • What is the direct DP and its cost?
  • What does the reversed question ask?

Hints

    Brute force, and why it fails

    Direct DP over every floor choice: O(e × f²).

    The key insight

    Counting floors covered by m drops grows fast and needs only one array, so the answer is found in O(e × answer).

    Dry run

    answer2

    e = 1, f = 2. One egg must try floors in order: 2 drops.

    Step 1 of 2

    The plan

    covered = zeros(e + 1); m = 0
    while covered[e] < f: m++; for k from e down to 1: covered[k] += covered[k - 1] + 1
    return m
    

    Complexity

    Time
    O(e × answer)
    Space
    O(e)

    The answer is at most f.

    Edge cases

    • 0 floors: 0.
    • 1 egg: f.
    • Many eggs: about log₂(f).
    I've built my logic, show the code

    Java

    class Solution {
        public int eggDrop(int e, int f) {
            int[] covered = new int[e + 1];         // covered[k]: the most floors k eggs can settle with the moves made so far
            int moves = 0;
            while (covered[e] < f) {                // one more drop: if it breaks, floors below; if not, floors above
                moves++;
                for (int k = e; k >= 1; k--) covered[k] = covered[k] + covered[k - 1] + 1;
            }
            return moves;
        }
    }

    C++

    class Solution {
    public:
        int eggDrop(int e, int f) {
            vector<long long> covered(e + 1, 0);    // covered[k]: the most floors k eggs can settle with the moves made so far
            int moves = 0;
            while (covered[e] < f) {                // one more drop: if it breaks, floors below; if not, floors above
                moves++;
                for (int k = e; k >= 1; k--) covered[k] = covered[k] + covered[k - 1] + 1;
            }
            return moves;
        }
    };

    Python

    class Solution:
        def eggDrop(self, e: int, f: int) -> int:
            covered, moves = [0] * (e + 1), 0       # covered[k]: the most floors k eggs settle with the moves made so far
            while covered[e] < f:                   # one more drop: if it breaks, floors below; if not, floors above
                moves += 1
                for k in range(e, 0, -1):
                    covered[k] = covered[k] + covered[k - 1] + 1
            return moves

    Common mistakes

    • Binary search with one egg.
    • Updating k upwards.

    Similar problems