Koko Eating Bananas LT875
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的更多相关文章
- 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 ...
- Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas)
Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas) 珂珂喜欢吃香蕉.这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉.警卫已经离开了,将在 H ...
- [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 ...
- [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 ...
- LeetCode 875. Koko Eating Bananas
原题链接在这里:https://leetcode.com/problems/koko-eating-bananas/ 题目: Koko loves to eat bananas. There are ...
- [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 ...
- 【LeetCode】875. Koko Eating Bananas 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- LeetCode Binary Search Summary 二分搜索法小结
二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- .sh_history文件的管理机制
来源:http://www.aixchina.net/Article/27258 字数 1056阅读 4365评论 1赞 0 内容提要: .sh_history是在ksh中用于存储用户在shell中输 ...
- angularjs 粘贴事件
参考 http://www.jb51.net/article/89708.htm ng-paste 需要setTimeout,否则无法获取到数据
- Linux下安装和使用nginx
浏览器和服务器的关系 NGINX nginx是什么 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件. nginx不但是一个优秀的web服务软件,还可以作为反向代理,负载均衡,以及 ...
- C/C++ typedef用法详解(真的很详细)
第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不符合我们的意图,它只声明了一个指向字符变量的指针, ...
- js阻止时间冒泡事件——event.stopPropagation()
1. 作用:不再派发事件. 2. 语法: html代码: <div class="oreder-cont" ng-click="Orderdetails()&quo ...
- TZOJ 3709:Number Maze(广搜记录前驱)
描述 You are playing one game called "Number Maze". The map of an example is shown in the fo ...
- f5 SSL及证书
1.SSL卸载 1)在BIG-IP上终结SSL连接BIG-IP可以全面了解应用,可以使用iRules, Profiles等,可以释放server的资源 2)包含:统一管理证书与密钥:支持基于硬件的关键 ...
- JDBC、ODBC、OLE DB、ADO、ADOMD区别与联系
ODBC: (Open Database Connectivity,开放数据库互连),它建立了一组规范,并提供了一组对数据库访问的标准API(应用程序编程接口).这些API利用SQL来完成其大部分任务 ...
- AngularJS——第9章 模块加载
第9章 模块加载 AngularJS模块可以在被加载和执行之前对其自身进行配置.我们可以在应用的加载阶段配置不同的逻辑. [AngularJS执行流程] 启动阶段(startup) 开始 --> ...
- RxJava笔记
网上搜索了一些关于 RxJava 的东西,对RxJava的定义自己理解如下: RxJava是要一种逻辑简洁的,通过一种扩展的观察者模式,来实现异步的一种链式编程.