[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 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
Python:
class Solution(object):
def minEatingSpeed(self, piles, H):
"""
:type piles: List[int]
:type H: int
:rtype: int
"""
tryEat = lambda s: sum(int(math.ceil(1.0 * p / s)) for p in piles)
left, right = 1, sum(piles)
while left <= right:
mid = (left + right) / 2
if tryEat(mid) <= H: right = mid - 1
else: left = mid + 1
return left
Python: wo
class Solution():
def eatBananas(self, piles, H):
piles = sorted(piles)
k = piles[-1]
res = float('Inf')
while k > 0:
t = H
for pile in piles:
if pile <= k:
t -= 1
else:
if pile % k == 0:
t -= pile / k
else:
t -= pile / k + 1
if t == 0:
res = min(res, k)
k -= 1
return res
All LeetCode Questions List 题目汇总
[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
原题链接在这里:https://leetcode.com/problems/koko-eating-bananas/ 题目: Koko loves to eat bananas. There are ...
- 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]之间 所以我们先二分把?找到 ...
- 猴猴吃香蕉 背包DP
猴猴吃香蕉 背包DP \(D\)次询问,第\(i\)次询问,每次有\(n_i\)个带权香蕉,问有多少方案使香蕉之积为\(k_i\),对结果取模\(1000000007\) \(n\le 10^3,k\ ...
随机推荐
- Codeforces G. Ciel the Commander
题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 修改cloud image密码
安装libguestfs-tools yum -y install libguestfs-tools.noarch 设置固定密码 virt-customize -a CentOS-7-x86_64-G ...
- Spring中的applicationContext文件详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 英语听力,如何成为更好的交谈着https://www.bilibili.com/video/av4279405?from=search&seid=5889429711390689339
and how many of you know at least one person that you because you just do not want to talk to them.y ...
- Redis 高可用架构设计(转载)
转载自:https://mp.weixin.qq.com/s?__biz=MzA3NDcyMTQyNQ==&mid=2649263292&idx=1&sn=b170390684 ...
- 计蒜之道 百度AI小课堂-上升子序列
计蒜之道 百度AI小课堂-上升子序列 题目描述 给一个长度为 \(n\) 的数组 \(a\) .试将其划分为两个严格上升子序列,并使其长度差最小. 输入格式 输入包含多组数据. 数据的第一行为一个正整 ...
- BZOJ 1213: [HNOI2004]高精度开根
二次联通门 : BZOJ 1213: [HNOI2004]高精度开根 正解 NTT+高精+倍增+二分 但是可以用python 2333333 m,n=int(raw_input()),int(raw_ ...
- JavaScript中字符串多行编辑
常用写法: var str = 'w3c' +'标准' +'方式.' 升级版:var str = ['w3c','标准','方式.'].join('');终极版:var str = 'w3c\标准\方 ...
- 打印出js对象里面的内容
最近调试的时候遇到需要打印出js对象里面的内容,两种方式: 1.直接使用 JSON.stringify(obj) 方法把对象转成字符串,打印出来.但是因为维护的项目比较老,使用的还是ie11的ie5兼 ...
- [后渗透]Metasploit使用基础
0x00 简介 Metasploit是一个免费的.可下载的框架,通过它可以很容易地获取.开发并对计算机软件漏洞实施攻击.它本身附带数百个已知软件漏洞的专业级漏洞攻击工具.当H.D. Moore在200 ...