【LeetCode】875. Koko Eating Bananas 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/koko-eating-bananas/description/
题目描述
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
题目大意
coco去吃香蕉,时间最多只有H小时,但这个吃货会享受,她想在用最慢的速度去吃。吃的速度是K,代表一个小时吃多少香蕉。这吃货懒到什么地步?当一个小时以内她就把这堆里边的香蕉吃完了,那她就停下来歇着,等待下一个小时继续吃。求她吃东西的最慢的速度。
解题方法
二分查找
二叉搜索的变种题目。因为有个最慢的速度而且是整数。
这个速度的范围一定在1和max(piles)之间,如果大于max(piles)肯定不是最慢速度。然后我们使用二分,计算在某个速度之下吃完的时间是否满足时间H。如果时间比H大,说明她吃的太快了,应该降低速度;反之应该加快速度。
吃每堆香蕉的时间是math.ceil(pile / speed)。
时间复杂度是O(logn),空间复杂度是O(1)。
Python代码如下:
class Solution:
def minEatingSpeed(self, piles, H):
"""
:type piles: List[int]
:type H: int
:rtype: int
"""
minSpeed, maxSpeed = 1, max(piles)
while minSpeed <= maxSpeed:
speed = minSpeed + (maxSpeed - minSpeed) // 2
hour = 0
for pile in piles:
hour += math.ceil(pile / speed)
if hour <= H:
maxSpeed = speed - 1
else:
minSpeed = speed + 1
return minSpeed
二刷的时候,使用了现在习惯的[left, right)左闭右开区间进行二分查找。代码如下:
class Solution(object):
def minEatingSpeed(self, piles, H):
"""
:type piles: List[int]
:type H: int
:rtype: int
"""
l, r = 1, sum(piles)
# [l, r)
while l < r:
K = l + (r - l) / 2
curH = 0
for p in piles:
curH += p // K + (1 if p % K else 0)
if curH > H:
l = K + 1
else:
r = K
return l
参考资料:
https://leetcode.com/problems/koko-eating-bananas/discuss/152506/Logical-Thinking-with-Java-Code
日期
2018 年 9 月 15 日 —— 天气转冷,小心着凉
2019 年 3 月 24 日 —— 二刷还是挺好的
【LeetCode】875. Koko Eating Bananas 解题报告(Python)的更多相关文章
- [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 ...
- 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】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas)
Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas) 珂珂喜欢吃香蕉.这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉.警卫已经离开了,将在 H ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- 标准非STL容器 : bitset
1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...
- mongoDB整个文件夹拷贝备份还原的坑
现网有一个mongoDB数据库需要搬迁到新服务器,开发那边的要求是先搬迁现在的数据库过去,然后剩下的以后他们用程序同步. 数据库大楷20G左右,现网是主备仲裁的,停掉备点,拷贝了全部文件. 新服务器也 ...
- 前端3 — js — BOM没完( 不了解也行 )
1.js是什么? -- 英文全称javascript javaScript(简称"JS") 是一种具有函数优先的轻量级,解释型或即时编译型的编程语言.虽然它是作为开发Web页面的脚 ...
- 使用 CliWrap 让C#中的命令行交互举重若轻
在代码中进行命令行交互是一个很常见的场景, 特别是在一些CI CD 自动化流程中, 在这之前我们会使用 System.Diagnostics.Process API, 现在有一个更灵活的工具 CliW ...
- JVM结构详解
JVM 结构详解 JVM 结构图 程序计数器(PC 寄存器) 程序计数器的定义 程序计数器是一块较小的内存空间,是当前线程正在执行的那条字节码指令的地址.若当前线程正在执行的是一个本地方法,那么此时程 ...
- day13 grep命令
day13 grep命令 linux三剑客之grep命令 介绍 grep(global search regular expression(RE) and print out the line,全面搜 ...
- 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常
1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...
- VIM中把^M替换为真正的换行符
:%s/\r/\r/g 或者:%s/^M/\r/g 红色的^M不是直接打出,而是按住ctrl再依次按下V和M
- Linux基础命令---mysql
mysql mysql是一个简单的sql shell,它可以用来管理mysql数据库. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 m ...
- 测试JDBCUtils的重用性
package cn.itcast.jdbc;import cn.itcast.util.JDBCUtils;import java.sql.*;import java.util.Properties ...