Problem

You are given two identical eggs and you have access to a building with n floors labeled from 1 to n.

You know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break , and any egg dropped at or below floor f will not break.

In each move, you may take an unbroken egg and drop it from any floor x (where 1 <= x <= n). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.

Return _theminimum number of moves that you need to determine with certainty what the value of _f is.

Examples

Example 1

1
2
3
4
5
6
Input: n = 2
Output: 2
Explanation: We can drop the first egg from floor 1 and the second egg from floor 2.
If the first egg breaks, we know that f = 0.
If the second egg breaks but the first egg didn't, we know that f = 1.
Otherwise, if both eggs survive, we know that f = 2.

Example 2

1
2
3
4
5
6
7
Input: n = 100
Output: 14
Explanation: One optimal strategy is:
- Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting from floor 1 and going up one at a time to find f within 8 more drops. Total drops is 1 + 8 = 9.
- If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9 and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more drops. Total drops is 2 + 12 = 14.
- If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45, 55, 64, 72, 79, 85, 90, 94, 97, 99, and 100.
Regardless of the outcome, it takes at most 14 drops to determine f.

Constraints

  • 1 <= n <= 1000

Solution

Method 1 – Mathematical Approach (Triangular Number)

Intuition

With 2 eggs, the optimal strategy is to minimize the worst-case number of moves. If we drop the first egg from increasing floors (x, x-1, x-2, …), we ensure that in the worst case, the total number of drops covers all floors. The minimum number of moves k satisfies k*(k+1)/2 >= n.

Approach

  1. Let k be the minimum number of moves such that k*(k+1)/2 >= n.
  2. Drop the first egg from floors 1, 2, …, k until it breaks (or you reach the top).
  3. If the first egg breaks at floor x, use the second egg to check floors one by one below x.
  4. The answer is the smallest k such that k*(k+1)/2 >= n.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    int twoEggDrop(int n) {
        int k = 0, sum = 0;
        while (sum < n) {
            k++;
            sum += k;
        }
        return k;
    }
};
1
2
3
4
5
6
7
8
func twoEggDrop(n int) int {
    k, sum := 0, 0
    for sum < n {
        k++
        sum += k
    }
    return k
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int twoEggDrop(int n) {
        int k = 0, sum = 0;
        while (sum < n) {
            k++;
            sum += k;
        }
        return k;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    fun twoEggDrop(n: Int): Int {
        var k = 0
        var sum = 0
        while (sum < n) {
            k++
            sum += k
        }
        return k
    }
}
1
2
3
4
5
6
7
8
class Solution:
    def twoEggDrop(self, n: int) -> int:
        k = 0
        sum_ = 0
        while sum_ < n:
            k += 1
            sum_ += k
        return k
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
impl Solution {
    pub fn two_egg_drop(n: i32) -> i32 {
        let mut k = 0;
        let mut sum = 0;
        while sum < n {
            k += 1;
            sum += k;
        }
        k
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    twoEggDrop(n: number): number {
        let k = 0, sum = 0;
        while (sum < n) {
            k++;
            sum += k;
        }
        return k;
    }
}

Complexity

  • ⏰ Time complexity: O(sqrt(n)), since the sum grows quadratically with k.
  • 🧺 Space complexity: O(1), only a few variables are used.