584.Drop Eggs II

There is a building of n floors. If an egg drops from the k th floor or above, it will break. If it's dropped from any floor below, it will not break.

You're given m eggs, Find k while minimize the number of drops for the worst case. Return the number of drops in the worst case.

Example
Given m = 2, n = 100 return 14
Given m = 2, n = 36 return 8

public class Solution {
/**
 * @param m the number of eggs
 * @param n the umber of floors
 * @return the number of drops in the worst case
 */
    int dropEggs2(int m, int n) {
    // Write your code here
    int[][] state = new int[n + 1][m + 1];
    for (int i = 1; i < m + 1; i++) {
        state[1][i] = 1;
    }
    for (int i = 1; i < n + 1; i++) {
        state[i][1] = i;
    }
    for (int i = 2; i < n + 1; i++) {//n ---> floor
        for (int j = 2; j < m + 1; j++) {//m ---> eggs
            int min = Integer.MAX_VALUE;
            for (int k = 1; k < i + 1; k++) {
               min = Math.min(Math.max(state[k - 1][j - 1], state[i - k][j]),min);
            }
            state[i][j] = min + 1;
        }
    }
    return state[n][m];
}
//f[t][p] = min(max(f[s - 1][p - 1]),f[t - s][p])|s=1...t) + 1
//总共t层,在s层扔p个蛋
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,127评论 0 23
  • 今天的关键词:批评 只要是人,我们在听到批评建议的时候都会不太舒服,俗话说忠言逆耳。就比如我的妈妈,她过去就很少赞...
    营养私教西西阅读 140评论 0 0
  • App: Hammerspoon 按下 Hyper + ` 就可以把鼠标移动到下一个屏幕正中间, 这样就不用长距离...
    sthtodo阅读 15,946评论 5 17
  • 麻疹? 高热,意识清楚。左眼睑红肿,脱皮,全身红斑,凸起,大小相对均等,手足较少。全身散在紫斑。口部溃疡,出血。 ...
    鱼丽枝阅读 244评论 0 0