- 🙂 第一次练习 我只能说这个题还是真的难,惹不起,我没看懂题目。
- 😄 第二次练习
# 动态规划
解题代码
class Solution {
public int superEggDrop(int K, int N) {
// Right now, dp[i] represents dp(1, i)
int[] dp = new int[N+1];
for (int i = 0; i <= N; ++i)
dp[i] = i;
for (int k = 2; k <= K; ++k) {
// Now, we will develop dp2[i] = dp(k, i)
int[] dp2 = new int[N+1];
int x = 1;
for (int n = 1; n <= N; ++n) {
// Let's find dp2[n] = dp(k, n)
// Increase our optimal x while we can make our answer better.
// Notice max(dp[x-1], dp2[n-x]) > max(dp[x], dp2[n-x-1])
// is simply max(T1(x-1), T2(x-1)) > max(T1(x), T2(x)).
while (x < n && Math.max(dp[x-1], dp2[n-x]) > Math.max(dp[x], dp2[n-x-1]))
x++;
// The final answer happens at this x.
dp2[n] = 1 + Math.max(dp[x-1], dp2[n-x]);
}
dp = dp2;
}
return dp[N];
}
}
# 易错点
- 易错项 1