1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class Solution {
public int superEggDrop(int K, int N) { int[][] dp = new int[K + 1][N + 1]; for (int m = 1; m <= N; m++) { dp[0][m] = 0; for (int k = 1; k <= K; k++) { dp[k][m] = dp[k][m - 1] + dp[k - 1][m - 1] + 1; if (dp[k][m] >= N) { return m; } } } return N; }
public int superEggDrop(int K, int N) { int[] dp = new int[N + 1]; for (int i = 0; i <= N; ++i) dp[i] = i;
for (int k = 2; k <= K; ++k) { int[] dp2 = new int[N + 1]; int x = 1; for (int n = 1; n <= N; ++n) { while (x < n && Math.max(dp[x - 1], dp2[n - x]) > Math.max(dp[x], dp2[n - x - 1])) x++;
dp2[n] = 1 + Math.max(dp[x - 1], dp2[n - x]); }
dp = dp2; }
return dp[N]; } }
|