Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The guards have gone and will come back in H hours.

Koko can decide her bananas-per-hour eating speed of K.  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.  If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.

Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.

Return the minimum integer K such that she can eat all the bananas within H hours.

Example 1:

Input: piles = [3,6,7,11], H = 8
Output: 4

Example 2:

Input: piles = [30,11,23,4,20], H = 5
Output: 30

Example 3:

Input: piles = [30,11,23,4,20], H = 6
Output: 23

Note:

    • 1 <= piles.length <= 10^4
    • piles.length <= H <= 10^9
    • 1 <= piles[i] <= 10^9

Idea 1. Binary search, similar to Capacity To Ship Packages Within D Days LT1011, search space: (ceiling(sum(piles)/h), max(piles))

ceiling(sum(piles)/h) = (sum(piles)-1)/h + 1

也可以免去一次遍历数组,直接设置search space (1, 10^9)

Time complexity: O(nlogw), n is the size of piles, w is the maximum of piles.

Space complexity: O(1)

 class Solution {
private int checkHours(int[] piles, int speed) {
int hours = 0;
for(int pile: piles) {
int hour = (pile-1)/speed + 1;
//int hour = ((pile%speed == 0) ? pile/speed : pile/speed + 1);
hours += hour;
}
return hours;
}
public int minEatingSpeed(int[] piles, int H) {
int min = 1, max = 0;
for(int pile: piles) {
max = Math.max(max, pile);
} while(min < max) {
int mid = min + (max - min)/2;
int hours = checkHours(piles, mid);
if(hours <= H) {
max = mid;
}
else {
min = mid + 1;
}
} return min;
}
}

Koko Eating Bananas LT875的更多相关文章

  1. 875. Koko Eating Bananas

    Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The g ...

  2. Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas)

    Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas) 珂珂喜欢吃香蕉.这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉.警卫已经离开了,将在 H ...

  3. [Swift]LeetCode875. 爱吃香蕉的珂珂 | Koko Eating Bananas

    Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i]bananas.  The gu ...

  4. [LeetCode] 875. Koko Eating Bananas 科科吃香蕉

    Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The g ...

  5. LeetCode 875. Koko Eating Bananas

    原题链接在这里:https://leetcode.com/problems/koko-eating-bananas/ 题目: Koko loves to eat bananas.  There are ...

  6. [LeetCode] 875. Koko Eating Bananas 可可吃香蕉

    Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The g ...

  7. 【LeetCode】875. Koko Eating Bananas 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  8. LeetCode Binary Search Summary 二分搜索法小结

    二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 字符串相似度算法(编辑距离Levenshtein Distance)的应用场景

    应用场景 DNA分析: 将DNA的一级序列如β-球蛋白基因的第一个外显子(Exon)转化为分子“结构图”,然后由所得“结构图”提取图的不变量,如分子连接性指数.以图的不变量作为自变量,再由相似度计算公 ...

  2. Tomcat添加管理员role

       最近朋友问我怎么在Tomcat里面使用 admin 登录,一般情况下登录后是提示xxx的,经过百度后,好不容易才找到答案:    原来添加一个role为admin:<role rolena ...

  3. jQuery 设置/获取样式

    参考 http://www.w3school.com.cn/jquery/jquery_css.asp $("#a").css("height"); $(&qu ...

  4. python--第四天总结

    lambda表达式 处理简单函数自动返回 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 == 1: name = 'wupeiqi' el ...

  5. 八 xml模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...

  6. C++批量注释代码段取消注释代码段快捷键

    1,   先选中要注释的代码段 2,按住ctrl+k+c注释本段代码 3,按住ctrl+k+u取消注释本段代码 用VS2013运行C++语言程序,运行结果闪一下就没了 解决方法是: 在return   ...

  7. PHP中的变量与PHP中算false的情况

    一PHP中的变量 1.PHP中的变量,声明与使用必须用$开头 2.PHP是一种弱类型语言,变量其实并不需要声明.可以直接给变量赋任何类型的值: 3.PHP中可以使用连等同时声明多个变量, eg:num ...

  8. MVC 的那点小事

    两年未见 一切从头再来.我猜到了故事的开头,找工作一如我想象的那般艰难,但是结果却比我预期的要好很多. 第一次开始用MVC 框架,比我想象的要简单的多,就像同事跟我说的,这只是个框架. 言归正传,前两 ...

  9. Data01-数据结构和算法绪论

    Data01-数据结构和算法绪论 一.数据结构和算法绪论 1.1 什么是数据结构? 数据结构是一门研究非数值计算的程序设计问题中的操作对象,以及它们之间的关系和操作等相关问题的学科. 程序设计=数据结 ...

  10. URL编码表、Base64编码表、HTTP消息含义

    URL编码表 backspace 8% A 41% a 61% § %A7 Õ %D5 tab 9% B 42% b 62% « %AB Ö %D6 linefeed %0A C 43% c 63% ...