|
| 1 | +# 887. Super Egg Drop |
| 2 | +You are given `k` identical eggs and you have access to a building with `n` floors labeled from `1` to `n`. |
| 3 | + |
| 4 | +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**. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Return *the **minimum number of moves** that you need to determine **with certainty** what the value of* `f` is. |
| 9 | + |
| 10 | +#### Example 1: |
| 11 | +<pre> |
| 12 | +<strong>Input:</strong> k = 1, n = 2 |
| 13 | +<strong>Output:</strong> 2 |
| 14 | +<strong>Explanation:</strong> |
| 15 | +Drop the egg from floor 1. If it breaks, we know that f = 0. |
| 16 | +Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1. |
| 17 | +If it does not break, then we know f = 2. |
| 18 | +Hence, we need at minimum 2 moves to determine with certainty what the value of f is. |
| 19 | +</pre> |
| 20 | + |
| 21 | +#### Example 2: |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> k = 2, n = 6 |
| 24 | +<strong>Output:</strong> 3 |
| 25 | +</pre> |
| 26 | + |
| 27 | +#### Example 3: |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> k = 3, n = 14 |
| 30 | +<strong>Output:</strong> 4 |
| 31 | +</pre> |
| 32 | + |
| 33 | +#### Constraints: |
| 34 | +* `1 <= k <= 100` |
| 35 | +* <code>1 <= n <= 10<sup>4</sup></code> |
| 36 | + |
| 37 | +## Solutions (Python) |
| 38 | + |
| 39 | +### 1. Solution |
| 40 | +```Python |
| 41 | +class Solution: |
| 42 | + @cache |
| 43 | + def superEggDrop(self, k: int, n: int) -> int: |
| 44 | + if n == 0: |
| 45 | + return 0 |
| 46 | + if k == 1: |
| 47 | + return n |
| 48 | + |
| 49 | + x = bisect_right(range(1, n + 1), 0, key=lambda x: self.superEggDrop(k - |
| 50 | + 1, x - 1) - self.superEggDrop(k, n - x)) + 1 |
| 51 | + ret = 1 + self.superEggDrop(k - 1, x - 1) |
| 52 | + if x > 0: |
| 53 | + ret = min(ret, 1 + self.superEggDrop(k, n - x + 1)) |
| 54 | + |
| 55 | + return ret |
| 56 | +``` |
0 commit comments