Skip to content

Latest commit

 

History

History

1884

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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 the minimum number of moves that you need to determine with certainty what the value of f is.

 

Example 1:

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:

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 7 more drops. Total drops is 1 + 7 = 8.
- 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

Companies:
Facebook, Bloomberg, Google

Related Topics:
Math, Dynamic Programming

Similar Questions:

Solution 1. DP

// OJ: https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N)
class Solution {
public:
    int twoEggDrop(int n) {
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= i; ++j) {
                dp[i] = min(dp[i], 1 + max(j - 1, dp[i - j]));
            }
        }
        return dp[n];
    }
};

Solution 2. Find Pattern

// n => answer
1: 1
2: 2
3: 2
4: 3
5: 3
6: 3
7: 4
8: 4
9: 4
10: 4
11: 5
12: 5
...

The pattern is that, for k drops, we can test up to 1 + 2 + ... + k floors.

Now we know we have n floors, we need to find the minimum k that 1 + 2 + ... + k >= n.

// OJ: https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/
// Author: github.com/lzl124631x
// Time: O(sqrt(N))
// Space: O(1)
class Solution {
public:
    int twoEggDrop(int n) {
        int ans = 0;
        while (n > 0) n -= ++ans;
        return ans;
    }
};

Solution 3. Math

We are looking for the minimum k that 1 + 2 + ... + k = k * (k + 1) / 2 >= n, so k^2 + k - 2n >= 0.

Based on the solution of quadratic equation

$$ x={\frac {-b\pm {\sqrt {b^{2}-4ac}}}{2a}} $$

We know k >= (-1 + sqrt(1 + 8 * n)) / 2.

// OJ: https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
public:
    int twoEggDrop(int n) {
        return ceil((-1 + sqrt(1 + 8 * n)) / 2); 
    }
};