Welcome to Subscribe On Youtube
887. Super Egg Drop
Description
You are given k 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.
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: k = 1, n = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know that f = 0. Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1. If it does not break, then we know f = 2. Hence, we need at minimum 2 moves to determine with certainty what the value of f is.
Example 2:
Input: k = 2, n = 6 Output: 3
Example 3:
Input: k = 3, n = 14 Output: 4
Constraints:
1 <= k <= 1001 <= n <= 104
Solutions
Solution 1: Depth-First Search + Memoization
We design a function $dfs(i, j)$, which means that when there are $i$ floors and $j$ eggs, the minimum number of operations to determine the $f$ value, then the answer is $dfs(n, k)$.
The execution logic of function $dfs(i, j)$ is as follows:
If $i \lt 1$, it means that the floor is less than or equal to $0$, and $0$ will be returned at this time;
If $j = 1$, it means there is only one egg, then you can only try layer by layer starting from the first layer. In the worst case, you need to try $i$ times, and then return $i$;
Otherwise, we consider the enumeration case where the first egg is dropped from layer $x$, where $1 \le x \le i$. If the egg breaks when dropped at the $x$ layer, it means $f \lt x$. At this time, we need to determine the $f$ value below the $x - 1$ layer and the remaining $j - 1$ eggs. The minimum number of operations required in total is $dfs(x - 1, j - 1) + 1$ times; if the egg is at the The $x$ layer was not broken when dropped, indicating $f \gt x$. At this time, we need to determine the $f$ value at the $x + 1$ layer and above and the remaining $j$ eggs. The total minimum number of operations required is $dfs(i - x, j) + 1$ times. Since we want to ensure the minimum number of operations in the worst case, $dfs(i, j) = \min_{1 \le x \le i} \max(dfs(x - 1, j - 1), dfs(i - x, j)) + 1$.
If enumerated in this way, since the number of states is $n \times k$ and each state needs to be enumerated $n$ times, the total time complexity will reach $O(n^2 \times k)$, which will exceed the time limit. We consider how to optimize.
We notice that the function $dfs(x - 1, j - 1)$ increases monotonically with the increase of $x$, while the function $dfs(i - x, j)$ decreases monotonically with the increase of $x$. Therefore, there is an optimal $x$ value that makes $\max(dfs(x - 1, j - 1), dfs(i - x, j))$ reach the minimum value. We can perform a binary search on $x$ to find the optimal $x$ value. Where $x$ is the largest integer that satisfies $dfs(x - 1, j - 1) \le dfs(i - x, j)$. This way we can reduce the time complexity to $O(n \times k \log n)$.
Time complexity $O(n \times k \log n)$, space complexity $O(n \times k)$. Among them, $n$ and $k$ are the number of floors and the number of eggs respectively.
Solution 2
We can also use dynamic programming to solve this problem.
When we define $f[i][j]$ to mean that there are $i$ floors and $j$ eggs, the minimum number of operations to determine the value of $f$ is then the answer is $f[n][k]$.
The state transition equation is $f[i][j] = \min_{1 \le x \le i} \max(f[x - 1][j - 1], f[i - x][j]) + 1$.
Similar to method one, we can use binary search to optimize the enumeration process of $x$.
Time complexity $O(n \times k \log n)$, space complexity $O(n \times k)$. Among them, $n$ and $k$ are the number of floors and the number of eggs respectively.
-
class Solution { private int[][] f; public int superEggDrop(int k, int n) { f = new int[n + 1][k + 1]; return dfs(n, k); } private int dfs(int i, int j) { if (i < 1) { return 0; } if (j == 1) { return i; } if (f[i][j] != 0) { return f[i][j]; } int l = 1, r = i; while (l < r) { int mid = (l + r + 1) >> 1; int a = dfs(mid - 1, j - 1); int b = dfs(i - mid, j); if (a <= b) { l = mid; } else { r = mid - 1; } } return f[i][j] = Math.max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1; } } // Solution 2 class Solution { public int superEggDrop(int k, int n) { int[][] f = new int[n + 1][k + 1]; for (int i = 1; i <= n; ++i) { f[i][1] = i; } for (int i = 1; i <= n; ++i) { for (int j = 2; j <= k; ++j) { int l = 1, r = i; while (l < r) { int mid = (l + r + 1) >> 1; int a = f[mid - 1][j - 1]; int b = f[i - mid][j]; if (a <= b) { l = mid; } else { r = mid - 1; } } f[i][j] = Math.max(f[l - 1][j - 1], f[i - l][j]) + 1; } } return f[n][k]; } } -
class Solution { public: int superEggDrop(int k, int n) { int f[n + 1][k + 1]; memset(f, 0, sizeof(f)); function<int(int, int)> dfs = [&](int i, int j) -> int { if (i < 1) { return 0; } if (j == 1) { return i; } if (f[i][j]) { return f[i][j]; } int l = 1, r = i; while (l < r) { int mid = (l + r + 1) >> 1; int a = dfs(mid - 1, j - 1); int b = dfs(i - mid, j); if (a <= b) { l = mid; } else { r = mid - 1; } } return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1; }; return dfs(n, k); } }; // Solution 2 class Solution { public: int superEggDrop(int k, int n) { int f[n + 1][k + 1]; memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { f[i][1] = i; } for (int i = 1; i <= n; ++i) { for (int j = 2; j <= k; ++j) { int l = 1, r = i; while (l < r) { int mid = (l + r + 1) >> 1; int a = f[mid - 1][j - 1]; int b = f[i - mid][j]; if (a <= b) { l = mid; } else { r = mid - 1; } } f[i][j] = max(f[l - 1][j - 1], f[i - l][j]) + 1; } } return f[n][k]; } }; -
class Solution: def superEggDrop(self, k: int, n: int) -> int: @cache def dfs(i: int, j: int) -> int: if i < 1: return 0 if j == 1: return i l, r = 1, i while l < r: mid = (l + r + 1) >> 1 a = dfs(mid - 1, j - 1) b = dfs(i - mid, j) if a <= b: l = mid else: r = mid - 1 return max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1 return dfs(n, k) # Solution 2 class Solution: def superEggDrop(self, k: int, n: int) -> int: f = [[0] * (k + 1) for _ in range(n + 1)] for i in range(1, n + 1): f[i][1] = i for i in range(1, n + 1): for j in range(2, k + 1): l, r = 1, i while l < r: mid = (l + r + 1) >> 1 a, b = f[mid - 1][j - 1], f[i - mid][j] if a <= b: l = mid else: r = mid - 1 f[i][j] = max(f[l - 1][j - 1], f[i - l][j]) + 1 return f[n][k] -
func superEggDrop(k int, n int) int { f := make([][]int, n+1) for i := range f { f[i] = make([]int, k+1) } var dfs func(i, j int) int dfs = func(i, j int) int { if i < 1 { return 0 } if j == 1 { return i } if f[i][j] != 0 { return f[i][j] } l, r := 1, i for l < r { mid := (l + r + 1) >> 1 a, b := dfs(mid-1, j-1), dfs(i-mid, j) if a <= b { l = mid } else { r = mid - 1 } } f[i][j] = max(dfs(l-1, j-1), dfs(i-l, j)) + 1 return f[i][j] } return dfs(n, k) } // Solution 2 func superEggDrop(k int, n int) int { f := make([][]int, n+1) for i := range f { f[i] = make([]int, k+1) } for i := 1; i <= n; i++ { f[i][1] = i } for i := 1; i <= n; i++ { for j := 2; j <= k; j++ { l, r := 1, i for l < r { mid := (l + r + 1) >> 1 a, b := f[mid-1][j-1], f[i-mid][j] if a <= b { l = mid } else { r = mid - 1 } } f[i][j] = max(f[l-1][j-1], f[i-l][j]) + 1 } } return f[n][k] } -
function superEggDrop(k: number, n: number): number { const f: number[][] = new Array(n + 1).fill(0).map(() => new Array(k + 1).fill(0)); const dfs = (i: number, j: number): number => { if (i < 1) { return 0; } if (j === 1) { return i; } if (f[i][j]) { return f[i][j]; } let l = 1; let r = i; while (l < r) { const mid = (l + r + 1) >> 1; const a = dfs(mid - 1, j - 1); const b = dfs(i - mid, j); if (a <= b) { l = mid; } else { r = mid - 1; } } return (f[i][j] = Math.max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1); }; return dfs(n, k); } // Solution 2 function superEggDrop(k: number, n: number): number { const f: number[][] = new Array(n + 1).fill(0).map(() => new Array(k + 1).fill(0)); for (let i = 1; i <= n; ++i) { f[i][1] = i; } for (let i = 1; i <= n; ++i) { for (let j = 2; j <= k; ++j) { let l = 1; let r = i; while (l < r) { const mid = (l + r + 1) >> 1; const a = f[mid - 1][j - 1]; const b = f[i - mid][j]; if (a <= b) { l = mid; } else { r = mid - 1; } } f[i][j] = Math.max(f[l - 1][j - 1], f[i - l][j]) + 1; } } return f[n][k]; }