1、中等的,两枚鸡蛋,检测n层最少需要几步
2、难的,k枚鸡蛋,检测n层最少需要几步
理解:1枚鸡蛋,一步只能检测一层,p【1】[n]=n;一枚鸡蛋检测n层需要n部;
2枚鸡蛋
1步肯定也只能检测一层;p[2][1]=1;
2步:一步,一枚鸡蛋换了,又变成一枚鸡蛋,一步了p[1][1]=1;一步没坏,就是p[2][1];同时验证了扔鸡蛋的这层;p[2][2]=p[1][1]+p[2][1]+1;
以此类推
k枚鸡蛋也是
代码如下:
中等的
class Solution {
public int twoEggDrop(int n) {
int maxCount=0;
int ret=0;
while(maxCount<n){
ret=ret+1;
maxCount=maxCount+ret;
}
return ret;
}
}
难的
class Solution {
public int superEggDrop(int k, int n) {
int[] p=new int[n+1];//节省空间
for(int i=0;i<=n;i++){
p[i]=i;
}
int[] b=new int[n+1];
int ret=n;
for(int i=2;i<=k;i++){
for(int j=1;j<=n;j++){
b[j]=p[j-1]+b[j-1]+1;
p[j-1]=b[j-1];
if(b[j]>=n){
ret=j;
break;
}
}
}
return ret;
}
}
哈哈哈