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. angular记录

    1. <h1>{{title}}</h1> 双花括号语法是 Angular 的插值绑定语法. 这个插值绑定的意思是把组件的 title 属性的值绑定到 HTML 中的 h1 标 ...

  2. javascript学习笔记(五):异常捕获和事件处理

    异常捕获 Try{ 发生异常的代码块 }catch(err){ 异常信息处理 } <!DOCTYPE html> <html> <head lang="en&q ...

  3. WeakReference 在android中的应用

    转:http://my.oschina.net/u/1177694/blog/523922 首先我们来看一段代码 public class AutoActivity extends Activity  ...

  4. Linux 字符设备驱动及一些简单的Linux知识

    一.linux系统将设备分为3类:字符设备.块设备.网络设备 1.字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流的设备,常见 ...

  5. WebSphere Application Server中manageprofiles的使用

    转自 https://www.cnblogs.com/lgfeng/archive/2013/02/21/2921215.html ---------------------------------- ...

  6. http://ctf.bugku.com/challenges#Easy_Re

      今天做一道逆向题,开心,见证了自己汇编的用途.     首先看它是否加壳? 1.加壳检测   是vc编程的,没有加壳,可以愉快地分析了.   2.分析程序,找到flag.   首先运行一下子程序, ...

  7. android.support.v4与Android.support.v7

    Android提供了android.support.v4和android.support.v7两个库,以便低版本API可以使用高版本API的功能. Fragment(碎片)类,是在Android 3. ...

  8. stm32中adc的常规通道和注入通道的区别

    STM32的每个ADC模块通过内部的模拟多路开关,可以切换到不同的输入通道并进行转换.STM32特别地加入了多种成组转换的模式,可以由程序设置好之后,对多个模拟通道自动地进行逐个地采样转换. 有2种划 ...

  9. Xcode调试与其他

    在项目中设置main接收的参数,模拟终端输入 Product->Scheme->Edit Scheme->Run->Arguments 例: 相当于在终端执行命令:./ac-t ...

  10. python------栈和队列的实现

    一.神马是栈 古有粮仓,即为栈.粮仓的特点就是最后放进去的谷粒,放在最上面.打仗行军,取粮是怎么取最快?肯定是最先取最上面的!栈这一种常用到的数据结构就是这种特点:后进先出(Last In First ...