LeetCode Solutions
887. Super Egg Drop
Time: $O(KN^2)$ Space: $O(KN)$
class Solution {
public:
int superEggDrop(int k, int n) {
// dp[k][n] := min # of moves to know f with k eggs and n floors
dp.resize(k + 1, vector<int>(n + 1, -1));
return drop(k, n);
}
private:
vector<vector<int>> dp;
int drop(int k, int n) {
if (k == 0) // No eggs -> done
return 0;
if (k == 1) // One egg -> drop from 1-th floor to n-th floor
return n;
if (n == 0) // No floor -> done
return 0;
if (n == 1) // One floor -> drop from that floor
return 1;
if (dp[k][n] != -1)
return dp[k][n];
dp[k][n] = INT_MAX;
for (int i = 1; i <= n; ++i) {
const int broken = drop(k - 1, i - 1);
const int unbroken = drop(k, n - i);
dp[k][n] = min(dp[k][n], 1 + max(broken, unbroken));
}
return dp[k][n];
}
};
class Solution {
public int superEggDrop(int k, int n) {
// dp[k][n] := min # of moves to know f with k eggs and n floors
dp = new int[k + 1][n + 1];
Arrays.stream(dp).forEach(row -> Arrays.fill(row, -1));
return drop(k, n);
}
private int[][] dp;
private int drop(int k, int n) {
if (k == 0) // No eggs -> done
return 0;
if (k == 1) // One egg -> drop from 1-th floor to n-th floor
return n;
if (n == 0) // No floor -> done
return 0;
if (n == 1) // One floor -> drop from that floor
return 1;
if (dp[k][n] != -1)
return dp[k][n];
dp[k][n] = Integer.MAX_VALUE;
for (int i = 1; i <= n; ++i) {
final int broken = drop(k - 1, i - 1);
final int unbroken = drop(k, n - i);
dp[k][n] = Math.min(dp[k][n], 1 + Math.max(broken, unbroken));
}
return dp[k][n];
}
}
class Solution {
public:
int superEggDrop(int k, int n) {
// dp[k][n] := min # of moves to know f with k eggs and n floors
dp.resize(k + 1, vector<int>(n + 1, -1));
return drop(k, n);
}
private:
vector<vector<int>> dp;
int drop(int k, int n) {
if (k == 0) // No eggs -> done
return 0;
if (k == 1) // One egg -> drop from 1-th floor to n-th floor
return n;
if (n == 0) // No floor -> done
return 0;
if (n == 1) // One floor -> drop from that floor
return 1;
if (dp[k][n] != -1)
return dp[k][n];
// broken[i] := drop(k - 1, i - 1) is increasing w/ i
// unbroken[i] := drop(k, n - i) is decreasing w/ i
// dp[k][n] := 1 + min(max(broken[i], unbroken[i])), 1 <= i <= n
// Find the first index i s.t broken[i] >= unbroken[i],
// Which minimizes max(broken[i], unbroken[i])
int l = 1;
int r = n + 1;
while (l < r) {
const int m = (l + r) / 2;
const int broken = drop(k - 1, m - 1);
const int unbroken = drop(k, n - m);
if (broken >= unbroken)
r = m;
else
l = m + 1;
}
return dp[k][n] = 1 + drop(k - 1, l - 1);
}
};