LeetCode 875. Koko Eating Bananas
原题链接在这里:https://leetcode.com/problems/koko-eating-bananas/
题目:
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^4piles.length <= H <= 10^91 <= piles[i] <= 10^9
题解:
If H >= piles.length. the worst case is that each hour, koko eats maxPile number of bananas, then she can eat all the bananas.
We could use binary search to try numbers between [1, maxPile].
When trying number mid, for current pile, accumlate how many hours it needs to eat this pile. ceil(pile/mid) = (pile+mid-1)/mid.
When total hours > H, then for each hour, eating mid is not enough, l = mid+1.
Otherwise, total hours <= H, then she could eat all bananas in H hours. Continue trying smaller number.
Note: (p + mid - 1) / mid, this is a good trick to get the ceil for integer.
Time Complexity: O(nlogm). n = piles.length. m = max(piles).
Space: O(1).
AC Java:
class Solution {
public int minEatingSpeed(int[] piles, int H) {
if(piles == null || piles.length > H){
return 0;
}
int maxPile = 0;
for(int p : piles){
maxPile = Math.max(maxPile, p);
}
int l = 1;
int r = maxPile;
while(l<r){
int mid = l + (r-l)/2;
int cost = 0;
for(int p : piles){
cost += (p+mid-1)/mid;
}
if(cost > H){
l = mid+1;
}else{
r = mid;
}
}
return l;
}
}
类似Minimize Max Distance to Gas Station, Capacity To Ship Packages Within D Days.
LeetCode 875. Koko Eating Bananas的更多相关文章
- [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 可可吃香蕉
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The g ...
- 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之二分法专题-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 ...
- 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 ...
- [leetcode] 875. 爱吃香蕉的珂珂(周赛)
875. 爱吃香蕉的珂珂 这题时间要求比较严格... 首先,将piles排序,然后二分查找. 总之,答案K肯定位于piles[?]piles[?+1]或者1piles[0]之间 所以我们先二分把?找到 ...
- LeetCode编程训练 - 折半查找(Binary Search)
Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...
随机推荐
- C++函数形参与实参交换
c++中函数的实参传递到形参的值是单向的,改变形参并不会影响实参. #include <iostream> using namespace std; void swap(int a, in ...
- 异常处理和UDP Socket套接字
一.异常处理 1.什么是异常处理: 程序在运行过程中出现了不可预知的错误,并且该错误没有对应的处理机制,那么就会以异常的形式表达出来,造成的影响就是整个程序无法再正常进行. 2.异常的结构: 1.异常 ...
- 如何在一个Docker中同时运行多个程序进程?
我们都知道Docker容器的哲学是一个Docker容器只运行一个进程,但是有时候我们就是需要在一个Docker容器中运行多个进程 那么基本思路是在Dockerfile 的CMD 或者 ENTRYPOI ...
- SpringBoot的入门程序
1. 创建一个springboot工程 可以参考springboot入门程序 2. 创建一个实体类 @Data //想相当于@Setter.@Getter和@ToString替代了setter.get ...
- 在Linux系统中创建SSH服务器别名
如果你经常通过 SSH 访问许多不同的远程系统,这个技巧将为你节省一些时间.你可以通过 SSH 为频繁访问的系统创建 SSH 别名,这样你就不必记住所有不同的用户名.主机名.SSH 端口号和 IP 地 ...
- kali之nmap
nmap简介 Nmap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包.可以扫描主机.端口.并且识别端口所对应的协议,以及猜测操作系统 Ping扫描(-sP参数) TCP ...
- vuecli3的项目搭建
1.卸载旧版本 npm uninstall vue-cli -g 或者 yarn global remove vue-cli 2.安装cli3脚手架 npm install -g @vue/cli 或 ...
- 使用原生JS 修改 DIV 属性
本例参考并改进自:https://www.jianshu.com/p/2961d9c317a3 大家可以一起学习!! <!DOCTYPE html> <html lang=" ...
- Spring AOP 原理的理解
>AOP基本概念 1)通知(Advice):织入到目标类连接点上的一段程序代码.通知分为五种类型: - Before:在方法被调用之前调用 - After:在方法完成后调用通知,无论方法是否执行 ...
- springboot多环境下maven打包
前言: 最近在项目中使用springboot时发现,采用在pom中定义不同的profile,并且maven打包时 采用-P参数并不能替换我application.properties文件中指定占位符的 ...