150强访谈简单

科科吃香蕉

“Koko 吃香蕉”问题的详细指南和 Python 实现。

问题陈述

简单

科科喜欢吃香蕉。有 n 堆香蕉,第 i 堆有 piles[i] 香蕉。警卫已经离开,并将在 h 小时后回来。

Koko 可以决定她每小时吃香蕉的速度为 k。每个小时,她都会选择一些香蕉并从那堆中吃 k 香蕉。如果这堆香蕉的数量少于 k ,她会吃掉所有香蕉,并且在这一小时内不会再吃更多的香蕉。

科科喜欢慢慢吃,但仍然想在守卫回来之前吃完所有香蕉。

返回最小整数 k ,以便她可以在 h 小时内吃掉所有香蕉。

编写一个函数 minEatingSpeed(piles: List[int], h: int) -> int

约束条件
  • 1 <= len(piles) <= 10^4
  • len(piles) <= h <= 10^9
  • 1 <= piles[i] <= 10^9

示例

Example 1
Input
piles = [3, 6, 7, 11], h = 8
Output
4
Explanation

At speed 4: pile 3 takes 1 hour, pile 6 takes 2 hours, pile 7 takes 2 hours, pile 11 takes 3 hours. Total = 8 hours.

Example 2
Input
piles = [30, 11, 23, 4, 20], h = 5
Output
30
Explanation

At speed 30: each pile takes 1 hour. Total = 5 hours.

Example 3
Input
piles = [30, 11, 23, 4, 20], h = 6
Output
23
Explanation

At speed 23: piles take 2+1+1+1+1 = 6 hours.

Need a Hint?
考虑使用二分搜索特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。