Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1884. Egg Drop With 2 Eggs and N Floors - Leetcode Solution

Code Implementation

class Solution:
    def twoEggDrop(self, n: int) -> int:
        # We need to find the minimal x such that x + (x-1) + ... + 1 >= n
        # That is, x(x+1)//2 >= n
        x = 0
        total = 0
        while total < n:
            x += 1
            total += x
        return x
      
class Solution {
public:
    int twoEggDrop(int n) {
        int x = 0, total = 0;
        while (total < n) {
            x++;
            total += x;
        }
        return x;
    }
};
      
class Solution {
    public int twoEggDrop(int n) {
        int x = 0, total = 0;
        while (total < n) {
            x++;
            total += x;
        }
        return x;
    }
}
      
var twoEggDrop = function(n) {
    let x = 0, total = 0;
    while (total < n) {
        x++;
        total += x;
    }
    return x;
};
      

Problem Description

You are given 2 identical eggs and access to a building with n floors labeled from 1 to n. Your goal is to determine the minimum number of moves required in the worst case to find the highest floor from which an egg can be dropped without breaking.

In one move, you can drop an egg from any floor. If the egg breaks, you cannot use it again. If it does not break, you can reuse it. You must minimize the number of moves required to guarantee finding the critical floor for any possible scenario.

  • You have exactly two eggs.
  • You must find the minimum number of moves needed in the worst case for any n floors.
  • Each egg can be dropped from any floor, and you cannot reuse an egg once it breaks.

Thought Process

To solve this problem, let's first consider a brute-force approach: you could drop the first egg from each floor starting from the first, but this could take up to n moves in the worst case, which is inefficient.

With two eggs, you want to minimize the number of moves required, even in the worst-case scenario. The key insight is to use the first egg to "narrow down" the range where the critical floor is, and then use the second egg to check each floor one by one within that range.

The challenge is to balance the floors you skip with the first egg with the number of remaining moves you might need with the second egg after the first one breaks. This leads to a mathematical approach to minimize the maximum number of moves.

Solution Approach

Let's break down the optimal strategy step by step:

  1. Drop the first egg from increasing floors:
    • Instead of dropping from every floor, drop the first egg from floors that increase by one less each time: first from floor x, then x + (x-1), then x + (x-1) + (x-2), and so on.
    • This way, if the first egg breaks at floor k, you have at most x-1 floors to check with the second egg.
  2. Mathematical formulation:
    • You want to find the smallest integer x such that x + (x-1) + ... + 1 ≥ n.
    • This sum is the triangular number formula: x(x+1)/2 ≥ n.
  3. Implementation:
    • Find the minimal x that satisfies the inequality above by incrementing x and summing until reaching or exceeding n.
    • Return x as the answer.

This approach ensures that, no matter where the critical floor is, you never need more than x moves.

Example Walkthrough

Let's walk through the process with n = 10 floors:

  1. Try to find the smallest x such that x(x+1)/2 ≥ 10:
    • x = 1: 1 < 10
    • x = 2: 3 < 10
    • x = 3: 6 < 10
    • x = 4: 10 = 10 (works!)
  2. So, x = 4. Drop the first egg from floor 4, then 7 (4+3), then 9 (7+2), then 10 (9+1).
  3. If the first egg breaks on floor 7, you know the critical floor is between 5 and 7, so you drop the second egg from floors 5, 6, and 7, one by one.
  4. In the worst case, you need at most 4 drops.

Time and Space Complexity

  • Brute-force approach: O(n) time and O(1) space, as you might need to check every floor.
  • Optimized approach (this solution): O(√n) time and O(1) space. Finding the minimal x such that x(x+1)/2 ≥ n takes up to √n iterations, which is much faster for large n.

The space complexity is constant since we only use a few variables.

Summary

The two-egg drop problem is a classic example of minimizing the worst-case number of moves using mathematical insight. By realizing that you can use the first egg to "divide and conquer" the search space and the second egg to check sequentially, you reduce the problem to finding the smallest x where the sum of the first x numbers is at least n. This approach ensures efficiency and is much faster than brute-force, especially as n grows.