java记忆化版本
class Solution {
Map<Integer,Integer> dictionary = new HashMap<Integer,Integer>();
public int superEggDrop(int k, int n) {
return dp(k,n);
}
public int dp(int k,int n){
if(dictionary.containsKey(n*100+k))
return dictionary.get(n*100+k);
if(k == 1){
dictionary.put(n*100+1,n);
return n;
}
if(n == 0){
dictionary.put(k,0);
return 0;
}
int result = Integer.MAX_VALUE;
int low = 1, high = n;
while(low <= high){
int mid = low + (high - low ) / 2;
int broken = dp(k - 1,mid - 1); // 鸡蛋碎了
int not_broken = dp(k , n - mid); // 鸡蛋没碎
if(broken > not_broken){
high = mid - 1;
result = Math.min(result,broken + 1);
}else{
low = mid + 1;
result = Math.min(result,not_broken+1);
}
}
dictionary.put(n*100+k,result);
return result;
}
}