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 ...
随机推荐
- 使用PLSQL客户端登录ORACLE时报ORA-12502和ORA-12545错误的解决方案
当使用plsqldev客户端登录oracle数据库时,如果对应的tnsnames.ora中是直接使用IP地址来连接,并且未在系统的hosts文件中绑定主机名时,极易出现ORA-12502及ORA-12 ...
- getline的字符串读入
也许是最近模拟题打多了的缘故,我发现自己渐渐变得比较毒瘤起来,当然这也是有一定的好处的,因为从中我也学到了一些处理字符串的正确姿势,今天我们就来讲一 讲如何用函数getline来读入一整行字符串进行处 ...
- Linux Centos6.5 升级默认Python2.6.6到Python2.7.13
以下例子基于python 2.7.9,其他版本同理.大致的命令都是差不多的,安装完成之后,输入Python --vertion ,看到系统默认的版本已经替换为2.7版本了 1.下载python wge ...
- 红帽RHEL6.8离线环境下升级到RHEL7.3
Red Hat Enterprise Linux 7 (RHEL 7) 是第一个支持从前一个 RHEL 主发行版本(RHEL 6)进行原位(in-place)升级的 RHEL 主版本.原位升级(in- ...
- Django学习笔记4-csrf防护
1.CSRF验证失败. 请求被中断. 原因是django为了在用户提交表单时防止跨站攻击所做的保护 什么是 CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求 ...
- Ubuntu之C++开发环境的搭建
初学Linux,今天反复卸载与重装微软商店的Ubuntu好几次,终于解锁了在Ubuntu上搭建C++开发环境的正确姿势, 搭建了一个非常简单的开发环境:简单到什么地步呢?只是简单地配置了一下编辑器,安 ...
- 初识Java——第一章 初识Java
1. 计算机程序: 为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合. 2. JAVA相关的技术: 1).安装和运行在本机上的桌面程序 2).通过浏览器访问的面向 ...
- Python入门 —— 01简介
Python 历史 python 的创始人为荷兰人吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ...
- 【mongodb分片中mogos启动的报错】
- 1. 了解HTML
HTML概念 HTML,超文本标记语言.它由一套标签组成用来描述网页,值得我们注意的是HTML并不是编程语言,它只是一种标记,我们通过HTML定义了网页的结构,然后再利用其他技术装饰这个结构,赋予这个 ...