(难题跳过)LeetCode 887.鸡蛋掉落
作者:互联网
题目(20¥)
题目地址:https://leetcode-cn.com/problems/super-egg-drop/
题解
难题跳过
源码
class Solution {
Map<Integer, Integer> memo = new HashMap<Integer, Integer>();
public int superEggDrop(int k, int n) {
return dp(k, n);
}
public int dp(int k, int n) {
if (!memo.containsKey(n * 100 + k)) {
int ans;
if (n == 0) {
ans = 0;
} else if (k == 1) {
ans = n;
} else {
int lo = 1, hi = n;
while (lo + 1 < hi) {
int x = (lo + hi) / 2;
int t1 = dp(k - 1, x - 1);
int t2 = dp(k, n - x);
if (t1 < t2) {
lo = x;
} else if (t1 > t2) {
hi = x;
} else {
lo = hi = x;
}
}
ans = 1 + Math.min(Math.max(dp(k - 1, lo - 1), dp(k, n - lo)), Math.max(dp(k - 1, hi - 1), dp(k, n - hi)));
}
memo.put(n * 100 + k, ans);
}
return memo.get(n * 100 + k);
}
}
标签:887,int,lo,memo,hi,ans,跳过,LeetCode,dp 来源: https://blog.csdn.net/Eazon_chan/article/details/118787099