Egg Dropping Puzzle

题目

答案
这个答案Leetcode TLE了,但是面试里面可以说。

class Solution {
    public int superEggDrop(int K, int N) {
        int[][] f = new int[N + 1][K + 1];
        
        for(int i = 1; i <= N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i == 1) {
                    f[i][j] = 1;
                    continue;
                }
                if(j == 1) {
                    f[i][j] = i;
                    continue;
                }
                int min = Integer.MAX_VALUE;
                for(int x = 1; x <= i - 1; x++) {
                    // Try to drop an egg on any floor below floor i
                    // It may not break, then f[i - x][j] is a candidate for f[i][j]
                    // it may break, then f[x - 1][j - 1] is a candidate for f[i][j]
                    // we want to take the max of the two candidates for worst case consideration
                    // but we also want to find what's the minimum number of attempts, so we take
                    // a minimum of all the maximums generated
                    int max = Math.max(f[i - x][j], f[x - 1][j - 1]) + 1;
                    min = Math.min(min, max);
                }
                f[i][j] = min;
            }
        }
        return f[N][K];
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 真是特别的一天 特别倒霉特别水逆 只庆幸阅读不是200 没过就算了呗做实验酒精灯爆炸是什么鬼 就当攒好运 不想去实...
    珍惜眼前始为真阅读 1,020评论 0 0
  • 如今已经三十有余了,才觉得人生还应有更高的追求。 我的人生中最幸福的时候,便是怀孕期间了。那时无忧无虑,什么都可以...
    有点阳光就灿烂的向日葵阅读 5,426评论 7 5
  • 23、资源枯竭,上哪里去找那么多的活动素材? 素材要从学生的生活实践中去挖掘。心理辅导课是为促进学生成长发展服务的...
    有福不享是傻子阅读 1,007评论 0 0
  • 歇业很久了,又开始找工作。除了app换了一个,一切似乎没有什么不同。只不过有了工作经验的我,对待面试这种东西的态度...
    世界名画本人阅读 3,599评论 0 4