博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lintcode584- Drop Eggs II- medium
阅读量:4591 次
发布时间:2019-06-09

本文共 1421 字,大约阅读时间需要 4 分钟。

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 = 2n = 100 return 14

Given m = 2n = 36 return 8

 
动态规划+递归。用二元数组存储某鸡蛋某层所需的次数。迭代试扔第一个鸡蛋,在某层扔。1.扔碎了即转为鸡蛋少一个,楼层少一层的子问题。2.没扔碎即转化为鸡蛋没有少楼层少为上半层那么多的子问题。
 
思维方式见:http://datagenetics.com/blog/july22012/index.html的
“Look see I can do three” 
 
 
public class Solution {    /*     * @param m: the number of eggs     * @param n: the number of floors     * @return: the number of drops in the worst case     */         public int dropEggs2(int m, int n) {        // write your code here        int[][] dp = new int[m + 1][n + 1];                for (int i = 1; i <= m; i++){            dp[i][0] = 0;            dp[i][1] = 1;        }                for (int j = 1; j <= n; j++){            dp[1][j] = j;        }                for (int i = 2; i <= m; i++){            for (int j = 2; j <= n; j++){                dp[i][j] = Integer.MAX_VALUE;                for(int k = 1; k < j; k++){                    dp[i][j] = Math.min(dp[i][j],                                        1 + Math.max(dp[i - 1][k - 1], dp[i][j - k]));                }            }        }        return dp[m][n];    }}

 

转载于:https://www.cnblogs.com/jasminemzy/p/7590257.html

你可能感兴趣的文章
springboot2.0 management.security.enabled无效
查看>>
spring cloud启动zipkin,报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings
查看>>
源发行版8需要目标发行版1.8
查看>>
Cleartext HTTP traffic to xxx not permitted解决办法
查看>>
[Docker] Win10中安装Docker并运行Nginx镜像
查看>>
pxe批量装机
查看>>
linux典型应用对系统资源使用的特点
查看>>
linux性能分析工具Procs
查看>>
linux性能分析工具Vmstat
查看>>
linux性能分析工具Memory
查看>>
c# 选择结构
查看>>
c# 委托
查看>>
c# 接口使用
查看>>
c# 事件
查看>>
c# as运算符
查看>>
c# 调试过程
查看>>
c# 结构
查看>>
C# 中的异常处理
查看>>
c# 调试
查看>>
c# 使用序列化
查看>>