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

901 篇文章 206 订阅

作者: 负雪明烛
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. 1 <= piles.length <= 10^4
  2. piles.length <= H <= 10^9
  3. 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 日 —— 二刷还是挺好的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于LeetCode上的问题994.腐烂的橘子,你可以使用Python来解决。下面是一个示例代码: ```python from collections import deque def orangesRotting(grid): # 记录网格的行数和列数 row, col = len(grid), len(grid[0]) # 定义四个方向:上、下、左、右 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 使用队列来保存腐烂的橘子的位置 queue = deque() # 记录新鲜橘子的数量 fresh_count = 0 # 遍历整个网格,初始化队列和新鲜橘子的数量 for i in range(row): for j in range(col): if grid[i][j] == 2: # 腐烂的橘子 queue.append((i, j)) elif grid[i][j] == 1: # 新鲜橘子 fresh_count += 1 # 如果新鲜橘子的数量为0,直接返回0 if fresh_count == 0: return 0 # 初始化分钟数 minutes = 0 # 开始进行BFS,直到队列为空 while queue: # 记录当前分钟数下,队列中的元素数量 size = len(queue) # 遍历当前分钟数下的所有腐烂的橘子 for _ in range(size): x, y = queue.popleft() # 遍历四个方向 for dx, dy in directions: nx, ny = x + dx, y + dy # 判断新位置是否在网格内,并且是新鲜橘子 if 0 <= nx < row and 0 <= ny < col and grid[nx][ny] == 1: # 将新鲜橘子变为腐烂状态 grid[nx][ny] = 2 # 将新鲜橘子的位置加入队列 queue.append((nx, ny)) # 新鲜橘子的数量减1 fresh_count -= 1 # 如果当前分钟数下,没有新鲜橘子了,结束循环 if fresh_count == 0: break # 每遍历完一层,分钟数加1 minutes += 1 # 如果最后还有新鲜橘子,返回-1,否则返回分钟数 return -1 if fresh_count > 0 else minutes ``` 你可以将给定的网格作为参数传递给`orangesRotting`函数来测试它。请注意,该代码使用了BFS算法来遍历橘子,并计算腐烂的分钟数。希望能对你有所帮助!如果有任何疑问,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值