887. Super Egg Drop
You are given
Keggs, and you have access to a building withNfloors from1toN.Each egg is identical in function, and if an egg breaks, you cannot drop it again.
You know that there exists a floor
Fwith0 <= F <= Nsuch that any egg dropped at a floor higher thanFwill break, and any egg dropped at or below floorFwill not break.Each move, you may take an egg (if you have an unbroken one) and drop it from any floor
X(with1 <= X <= N).Your goal is to know with certainty what the value of
Fis.What is the minimum number of moves that you need to know with certainty what
Fis, regardless of the initial value ofF?
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: 3Example 3:
Input: K = 3, N = 14
Output: 4
Note:
1 <= K <= 1001 <= 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的更多相关文章
- [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 ...
- 【LeetCode】887. Super Egg Drop 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...
- Leetcode 887 Super Egg Drop(扔鸡蛋) DP
这是经典的扔鸡蛋的题目. 同事说以前在uva上见过,不过是扔气球.题意如下: 题意: 你有K个鸡蛋,在一栋N层高的建筑上,被要求测试鸡蛋最少在哪一层正好被摔坏. 你只能用没摔坏的鸡蛋测试.如果一个鸡蛋 ...
- LeetCode 887. Super Egg Drop
题目链接:https://leetcode.com/problems/super-egg-drop/ 题意:给你K个鸡蛋以及一栋N层楼的建筑,已知存在某一个楼层F(0<=F<=N),在不高 ...
- [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 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- 膜 社论(egg drop)
题面 \(n\) 楼 \(m\) 个鸡蛋,从 \(k\) 楼及以上扔下去会碎,不能再测试 . 问至少需要扔几次确定 \(k\) . \(n\le 10^{18}\),\(m\le 64\) . 题解 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- hibernate编写流程
1.加载hibernatexml配置文件 2.创建sessionFactory 3.根据sessionFactory创建session 4.开启事务 5.持久化操作 6.提交事务 7.释放资源 其中第 ...
- Castle ActiveRecord学习(二)配置、引用、程序启动
来源:http://www.cnblogs.com/zxj159/p/4082987.html 配置数据库驱动: Model层引用:Castle.ActiveRecord.dll.NHibernate ...
- Linux readelf命令
一.简介 readelf用来显示一个或者多个elf格式的目标文件的信息,可以通过它的选项来控制显示哪些信息.这里的elf-file(s)就表示那些被检查的文件.可以支持32位,64位的elf格式文件, ...
- loadrunner12-查看controller运行报错详细log
1.路径为controller-->results-->results setting 2.打开文件夹res/log/***.log,里面会有当前场景运行的log日志. 注:启用这个首先保 ...
- Stripies
/* Our chemical biologists have invented a new very useful form of life called stripies (in fact, th ...
- MEME(Motif-based sequence analysis tools)使用说明
MEME(Motif-based sequence analysis tools)使用说明 2011-05-27 ~ ADMIN MEME是用于从一堆序列中搜索功能结构域的工具.比如说当你拿到了许多C ...
- Laravel 认证原理及完全自定义认证
Laravel 默认的 auth 功能已经是很全面了,但是我们也经常会碰到一些需要自定义的一些情况,比如验证的字段和默认的不匹配,比如需要能够同时满足 user name 和 email 认证等等.如 ...
- 2018.09.28 bzoj1563: [NOI2009]诗人小G(决策单调性优化dp)
传送门 决策单调性优化dp板子题. 感觉队列的写法比栈好写. 所谓决策单调性优化就是每次状态转移的决策都是在向前单调递增的. 所以我们用一个记录三元组(l,r,id)(l,r,id)(l,r,id)的 ...
- 2018.08.29 NOIP模拟 table(拓扑排序+建图优化)
[描述] 给出一个表格,N 行 M 列,每个格子有一个整数,有些格子是空的.现在需要你 来做出一些调整,使得每行都是非降序的.这个调整只能是整列的移动. [输入] 第一行两个正整数 N 和 M. 接下 ...
- 2018.07.31 bzoj4569: [Scoi2016]萌萌哒(并查集+倍增)
传送门 对于每个限制,使用倍增的二进制拆分思想,用并查集数组fa[i][j]" role="presentation" style="position: rel ...