254. Drop Eggs【LintCode java】
Description
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 two eggs, Find k while minimize the number of drops for the worst case. Return the number of drops in the worst case.
Clarification
For n = 10, a naive way to find k is drop egg from 1st floor, 2nd floor ... kth floor. But in this worst case (k = 10), you have to drop 10 times.
Notice that you have two eggs, so you can drop at 4th, 7th & 9th floor, in the worst case (for example, k = 9) you have to drop 4 times.
Example
Given n = 10
, return 4
.
Given n = 100
, return 14
.
解析:https://blog.csdn.net/shaya118/article/details/40823225
代码如下:
public class Solution {
/**
* @param n: An integer
* @return: The sum of a and b
*/
public int dropEggs(int n) {
// write your code
long times = 1;
if(n == 2)
return 2;
for(long i = 1; i < n; i++){
if((i+1)*i/2 >= n){
times = i;
break;
}
}
return (int)times;
}
}
254. Drop Eggs【LintCode java】的更多相关文章
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 413. Reverse Integer【LintCode java】
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...
随机推荐
- x+=i和x = x+i比较 -- 简单赋值和复合赋值
这两个赋值方式其实是有区别的,如果最后结果的类型和左操作数的类型一样,那么这两个表达式就完全等价. 下面来看看两个例子来理解它们的区别: 编写一个程序,使得x+=i合法, x = x+i: 不合法. ...
- webservice 从客户端中检测到有潜在危险的 Request.Form 值
webservice中传递xml格式的参数时报错 webservice 从客户端中检测到有潜在危险的 Request.Form 值解决方案: 1.在web.config的system.web节点中添加 ...
- MVC验证码生成类库
public class ValidateCode { /// <summary> /// 验证码的最大长度 /// </summary> public int MaxLeng ...
- ARM 汇编指令集 特点之一:指令后缀
1.同一 指令 添加不同的后缀 就会有不同的功能! 例子: B (Byte) 功能不变,操作长度变为8位 H (Half Word) 功能不变,操作长度变为16位 S(Signed) 功能不变,操作 ...
- JAVA格式化解析日期
- RAC初体验(环境搭建)
实施阶段: 1.主机配置 2.安装Clusterware 3.安装Oracle Database 4.配置Listener 5.创建ASM 6.创建Database 一.主机配置 1.网络设置 I ...
- React 父子组件和非父子组件传值
零.this.props 可以接收到 外界的传值 和 此组件标签内部自定义的方法 例: <one vals={message} sendVal={this ...
- QWebView 与Js 交互
我本愚钝,在网上搜了一下没找到可以运行的栗子,遂在这记录一下吧. 环境:win10 64位系统 qt 4.8.7 (mingw32) qtcreator(4.5.0) 1. 建立一个 Widgets ...
- hive常见的几种优化手段
Hive调优的几个入手点: Hive是基于Hadoop框架的,Hadoop框架又是运行在JVM中的,而JVM最终是要运行在操作系统之上的,所以,Hive的调优可以通过如下几个方面入手: 操作系统调优 ...
- 【AD】自己画板的备忘
快捷键: [Ctrl + M ]计算出两点之间的距离,画电路板时会用到 [Ctrl + Q ]在设定X.Y..等等的地方,快捷键可以公英制快速切换 [shift + 空格键 ]在布线的同时,此快捷键可 ...