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. 游戏AI技术 2

    [Unity3D人工智能编程精粹 2] 1.跟随领队行为. 用靠近(Seek)或追逐(Pursuit)实现跟随领队行为并不好.在Seek中,AI角色会被推向领队,最终与领队占据相同位置.而Pursui ...

  2. Real Time Render 4

    [Real Time Render 4] 1.Radiometry(辐射测试) deals with the measurement of electromagnetic(电磁) radiation( ...

  3. redis做消息列队

    #encoding:utf8 import time import redis conn = redis.Redis('localhost',db=1) #连接诶数据库并使用数据库1 def inse ...

  4. SQL%ROWCOUNT作用

    SQL%ROWCOUNT是一个游标属性,而SQL中的DML操作实际上是一种隐式的游标操作,在做insert,update,delete,merge以及select into操作时,Oracle会打开一 ...

  5. CSS float清除浮动

    解决高度塌陷的问题 – 清除浮动 CSS中有个讨论较多的话题就是如何清除浮动,清除浮动其实就一个目的,就是解决高度塌陷的问题.为什么会高度塌陷?什么时候会高度塌陷?塌陷原因是:元素含有浮动属性 – 破 ...

  6. 最长公共子序列lcs 51nod1006

    推荐参考博客:动态规划基础篇之最长公共子序列问题 - CSDN博客  https://blog.csdn.net/lz161530245/article/details/76943991 个人觉得上面 ...

  7. 贪吃蛇Ground Java实现(二)

    package cn.tcc.snake.antition; import java.awt.Color;import java.awt.Graphics; import java.awt.Point ...

  8. 导入Unity插件时出现Failed to import package with error: Couldn't decompress package

    导入Unity插件时出现Failed to import package with error: Couldn't decompress package 一开始以为压缩包本身有问题,坏了 后来发现在父 ...

  9. cisco 3850 GBIC报错处理

    今天有用户cisco 3850插入多模千兆光模块后报错日志如下: *Oct 18 13:48:54: %PLATFORM_PM-6-MODULE_ERRDISABLE:The inserted SFP ...

  10. [剑指Offer]54-二叉搜索树的第k个节点

    题目描述 给定一棵二叉搜索树,找出其中的第k小的结点,返回指向该节点的指针. 思路 中序遍历即可. 注意特判!报段错误数组越界这里就要考虑是少特判的问题. 法一:借助vector 法二(better) ...