|
| 1 | +# [1884. Egg Drop With 2 Eggs and N Floors](https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors) |
| 2 | + |
| 3 | +[中文文档](/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given <strong>two identical</strong> eggs and you have access to a building with <code>n</code> floors labeled from <code>1</code> to <code>n</code>.</p> |
| 8 | + |
| 9 | +<p>You know that there exists a floor <code>f</code> where <code>0 <= f <= n</code> such that any egg dropped at a floor <strong>higher</strong> than <code>f</code> will <strong>break</strong>, and any egg dropped <strong>at or below</strong> floor <code>f</code> will <strong>not break</strong>.</p> |
| 10 | + |
| 11 | +<p>In each move, you may take an <strong>unbroken</strong> egg and drop it from any floor <code>x</code> (where <code>1 <= x <= n</code>). If the egg breaks, you can no longer use it. However, if the egg does not break, you may <strong>reuse</strong> it in future moves.</p> |
| 12 | + |
| 13 | +<p>Return <em>the <strong>minimum number of moves</strong> that you need to determine <strong>with certainty</strong> what the value of </em><code>f</code> is.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong>Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> n = 2 |
| 20 | +<strong>Output:</strong> 2 |
| 21 | +<strong>Explanation:</strong> We can drop the first egg from floor 1 and the second egg from floor 2. |
| 22 | +If the first egg breaks, we know that f = 0. |
| 23 | +If the second egg breaks but the first egg didn't, we know that f = 1. |
| 24 | +Otherwise, if both eggs survive, we know that f = 2. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong>Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> n = 100 |
| 31 | +<strong>Output:</strong> 14 |
| 32 | +<strong>Explanation:</strong> One optimal strategy is: |
| 33 | +- Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting |
| 34 | + from floor 1 and going up one at a time to find f within 7 more drops. Total drops is 1 + 7 = 8. |
| 35 | +- If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9 |
| 36 | + and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more |
| 37 | + drops. Total drops is 2 + 12 = 14. |
| 38 | +- If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45, |
| 39 | + 55, 64, 72, 79, 85, 90, 94, 97, 99, and 100. |
| 40 | +Regardless of the outcome, it takes at most 14 drops to determine f. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= n <= 1000</code></li> |
| 48 | +</ul> |
| 49 | + |
| 50 | + |
| 51 | +## Solutions |
| 52 | + |
| 53 | +<!-- tabs:start --> |
| 54 | + |
| 55 | +### **Python3** |
| 56 | + |
| 57 | +```python |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +### **Java** |
| 62 | + |
| 63 | +```java |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +### **...** |
| 68 | + |
| 69 | +``` |
| 70 | +
|
| 71 | +``` |
| 72 | + |
| 73 | +<!-- tabs:end --> |
0 commit comments