You are given K eggs, and you have access to a building with N floors from 1 to N.

Each egg is identical in function, and if an egg breaks, you cannot drop it again.

You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break.

Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).

Your goal is to know with certainty what the value of F is.

What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?

Example 1:

Input: K = 1, N = 2
Output: 2
Explanation:
Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.

Example 2:

Input: K = 2, N = 6
Output: 3

Example 3:

Input: K = 3, N = 14
Output: 4

Note:

  1. 1 <= K <= 100
  2. 1 <= N <= 10000

Approach #1: DP. [C++][TLE][O(K*N^2)]

class Solution {
public:
int superEggDrop(int K, int N) {
int c = 0;
vector<vector<int>> dp(K+1, vector<int>(N+1, 0));
for (int i = 1; i <= N; ++i) dp[1][i] = i;
for (int i = 2; i <= K; ++i) {
for (int j = 1; j <= N; ++j) {
dp[i][j] = INT_MAX;
for (int k = 1; k <= j; ++k) {
c = 1 + max(dp[i-1][k-1], dp[i][j-k]);
if (c < dp[i][j])
dp[i][j] = c;
}
}
}
return dp[K][N];
}
};

  

Approach #2: DP. [Java]

class Solution {
public int superEggDrop(int K, int N) {
int[][] dp = new int[N+1][K+1];
int m = 0;
while (dp[m][K] < N) {
++m;
for (int k = 1; k <= K; ++k)
dp[m][k] = dp[m-1][k-1] + dp[m-1][k] + 1;
} return m;
}
}

  

Analysis:

Firstly, if we have K eggs and s steps to detect a buliding with Q(k, s) floors.

Secondly, we use 1 egg and 1 step to detect one floor,

if egg break, we can use (k-1) eggs and (s-1) to detect with Q(k-1, s-1),

if egg isn't broken, we can use k eggs and (s-1) step to detech with Q(k, s-1),

So, Q(k,s) = 1 + Q(k, s-1) + Q(k-1, s-1);

dp[i] is max floors we can use i eggs and s step to detect.

Reference:

https://leetcode.com/problems/super-egg-drop/discuss/159508/easy-to-understand

887. Super Egg Drop的更多相关文章

  1. [LeetCode] 887. Super Egg Drop 超级鸡蛋掉落

    You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is iden ...

  2. 【LeetCode】887. Super Egg Drop 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...

  3. Leetcode 887 Super Egg Drop(扔鸡蛋) DP

    这是经典的扔鸡蛋的题目. 同事说以前在uva上见过,不过是扔气球.题意如下: 题意: 你有K个鸡蛋,在一栋N层高的建筑上,被要求测试鸡蛋最少在哪一层正好被摔坏. 你只能用没摔坏的鸡蛋测试.如果一个鸡蛋 ...

  4. LeetCode 887. Super Egg Drop

    题目链接:https://leetcode.com/problems/super-egg-drop/ 题意:给你K个鸡蛋以及一栋N层楼的建筑,已知存在某一个楼层F(0<=F<=N),在不高 ...

  5. [Swift]LeetCode887. 鸡蛋掉落 | Super Egg Drop

    You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is ident ...

  6. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  7. 膜 社论(egg drop)

    题面 \(n\) 楼 \(m\) 个鸡蛋,从 \(k\) 楼及以上扔下去会碎,不能再测试 . 问至少需要扔几次确定 \(k\) . \(n\le 10^{18}\),\(m\le 64\) . 题解 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. 只有自己看的懂的vue 二叉树的3级联动

    我是在vue做的数据 actions mutations state index页面获取值 传递给子页面 子页面的操作 <template> <div class='cascade_ ...

  2. 在windows系统下安装oracle 11g

     oracle 11g 安装在windows server 2012 系统下. 最近,需要配置数据库,要求在windows操作系统下,安装oracle 11g 数据库,因为以前没有安装过,所以成功后, ...

  3. vs不同

    写了很多却错误关闭,无语,直接上内容,因为在公司年限长和德国.波兰.英国公司都有合作,而且他们的开发工具各不相同,因此我电脑上有Visual Studio 2008,Visual Studio 201 ...

  4. 社交类APP原型模板分享——Tinder

    Tinder是国外的一款手机交友APP,作用是基于用户的地理位置,每天“推荐”一定距离内的四个对象,根据用户在 Facebook 上面的共同好友数量.共同兴趣和关系网给出评分,得分最高的推荐对象优先展 ...

  5. kafka系列 -- 多线程消费者实现

    看了一下kafka,然后写了消费Kafka数据的代码.感觉自己功力还是不够. 不能随心所欲地操作数据,数据结构没学好,spark的RDD操作没学好. 不能很好地组织代码结构,设计模式没学好,面向对象思 ...

  6. delphi XE7 数组操作中缺少的find(POS)功能

    delphi xe7 中对数组操作做了很多扩充,比如加入了类似字符串处理的功能. 例如,数组相加 var A: array of integer; B: TBytes = [1,2,3,4]; //I ...

  7. 2018.07.31cogs2964. 数列操作η(线段树)

    传送门 线段树基本操作. 给出一个排列b,有一个初始值都为0的数组a,维护区间加1,区间统计区间∑(ai/bi)" role="presentation" style=& ...

  8. python3中 for line1 in f1.readlines():,for line1 in f1:,循环读取一个文件夹

    循环读取一个文件: fr.seek(0) fr.seek(0, 0) 概述 seek() 方法用于移动文件读取指针到指定位置. 语法 seek() 方法语法如下: fileObject.seek(of ...

  9. 在 web 容器中运行 cxf

    <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC &q ...

  10. chrome添加 postman扩展程序图文简介

    Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件.无论是web前端开发 或 android.ios开发,只要涉及调用后端接口,postman这类型工具就必不可少了.相对于 ...