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

Approach #1:

class Solution {
public:
int minEatingSpeed(vector<int>& piles, int H) {
int len = piles.size();
if (len == 1) return 1;
long long l = 0, r = pow(10, 9);
while (l < r) { // not l <= r
long long mid = l + (r - l) / 2;
int time = 0;
for (int i = 0; i < len; ++i)
time += (piles[i] - 1) / mid + 1; // better than time += (piles[i] % mid) == 0 ? piles[i] / mid : piles[i] / mid + 1;
if (time > H) l = mid + 1;
else r = mid; // mot r = mid - 1
}
return l;
}
};
Runtime: 68 ms, faster than 38.03% of C++ online submissions for Koko Eating Bananas.

 

Analysis:

Pay attention to the boundary condition.

875. Koko Eating Bananas的更多相关文章

  1. [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 ...

  2. LeetCode 875. Koko Eating Bananas

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

  3. [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 ...

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

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

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

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

  6. [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 ...

  7. Koko Eating Bananas LT875

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

  8. LeetCode编程训练 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  9. 算法与数据结构基础 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

随机推荐

  1. TFTP服务器

    为什么要学习有关TFTP服务器的安装及配置呢?主要是为了后续学习有关linux系统的无人值守安装做准备. TFTP简单文件传输协议,使用UDP的69端口.主要提供文件的上传和下载,TFTP一般是适用于 ...

  2. C#中使用byte[]数据,生成Bitmap

    /// <summary> /// 使用byte[]数据,生成256色灰度 BMP 位图 /// </summary> /// <param name="ori ...

  3. C# json反序列化 对象中嵌套数组 (转载) 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

    C# json反序列化 对象中嵌套数组 (转载)   看图: 这里可以看到是二层嵌套!!使用C#如何实现?? 思路:使用list集合实现 → 建立类 → list集合 → 微软的   Newtonso ...

  4. Pycharm下运行程序查看每个变量的值的方法(类似于Spyder和MATLAB)

    昨天,用了大量篇幅讲了Spyder的各种问题,之所以要用Spyder,最重要的一个原因就是能够非常方便的查看中间变量的值.类似MATLAB的工作空间,非常方便.如下图所示: 但是Spyder的代码自动 ...

  5. Java核心技术之基本数据类型

    这篇文章.我们讨论一些java的最主要的东西.这些东西我们一般刚刚学java的时候就学过,可是不一定真正明确. 正好,我在做一个读取内存的值,涉及到bit位的值的读取和写.那就能够讨论一个java的基 ...

  6. LeetCode 226 Invert Binary Tree(转换二叉树)

    翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...

  7. 序列化模块 json pickel shelve

    一.json  模块 1.定义 将字典.列表等内容转换成字符串的过程就是序列化. 操作的数据类型有限,但是可以支持所有编程语言操作. 2.为什么要有序列化? 1.以某种存储形式使自定义对象持久化. 2 ...

  8. bash shell和进程

    1 exec builtin 不创建子shell,在原进程的上启动新的脚本,但是它会把老shell的环境清理掉,所以,它从原shell中什么也不继承,在一个干净的环境中执行新的脚本.执行完之后退出当前 ...

  9. How do I set the timeout for a JAX-WS webservice client?

    How do I set the timeout for a JAX-WS webservice client? up vote58down votefavorite 27 I've used JAX ...

  10. Java 内存区域与内存溢出异常

    一.Java虚拟机内存划分 1.程序计数器 线程私有 可以看做是当前线程所执行的字节码的行号指示器.字节码解释器工作时是通过改变这个计数器的值来选取下一条需要执行的字节码指令. Java虚拟机是通过多 ...