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. ajax.beginform控制器中实体为null的问题

    控制器: 函数声明:public JsonResult ApplyFun(Test test) 原因:在视图中有一个表单的name属性为test,因为冲突所导致.

  2. Linux tcpdump命令

    一.简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据 ...

  3. DNA binding motif比对算法

    DNA binding motif比对算法 2012-08-31 ~ ADMIN 之前介绍了序列比对的一些算法.本节主要讲述motif(有人翻译成结构模式,但本文一律使用基模)的比对算法. 那么什么是 ...

  4. 那些我离不开的 Sketch 插件

    当谈论到插件时,我是一名极客.各种新颖的 Sketch 插件层出不穷,但是有那么几个是我怎么也离不开的. Sketch 运行器 多层的插件菜单再也不会影响我的效率了. 我推迟了好几年才使用这个插件,因 ...

  5. 用Notepad++在文本文件里快速在每行头尾都加上指定的内容(转载)

  6. Realtek 8192cu 支持 Android Hotspot 软ap

    http://www.cnblogs.com/bpasser/archive/2011/10/15/2213483.html Android 2.2 开始增加了WiFi Hotspot,可将Andro ...

  7. ubuntu14.04 Samba服务无法访问 可能没有权限 指定的网络名不再可用的问题

    按常规配置后,在windows资源管理器中登陆samba服务器,看得到分享目录却无法打开,弹出"无法访问.您可能没有权限使用网络资源,请与这台服务器的管理员联系以查明您是否有访问权限.指定的 ...

  8. KbmMW 服务器架构简介

    kbmmw 由于文档比较少,很多同学开始用时很难理解.一直准备写一个关于kbmmw 架构的东西. 这几天与红鱼儿(blog)研究服务器线程时,整理了一下,大概画了一下kbmmw (版本4.5)服务器的 ...

  9. 面向对象先修:Java入门

    学习总结 在C语言和数据结构的基础上,在上暑期的面向对象Java先修课程时,熟悉语言的速度明显加快了很多.Java和C在很多基础语法上非常相似,比如基本的数据类型,循环以及条件分支语句,数组的遍历等. ...

  10. flask_web表单

    一.配置 1.为了能够处理 web 表单,我们将使用 Flask-WTF,该扩展封装了 WTFForms 并且恰当地集成进 Flask 中.许多 Flask 扩展需要大量的配置,因此我们将要在 mic ...