NeetCode #276LC-875MediumBinary SearchNC 150NC 250
← Back to All Problems#276 · #875 · Koko Eating Bananas(爱吃香蕉的珂珂)
📌 Problem Statement & Constraints
Koko has
piles of bananas and h hours. Each hour she chooses a pile and eats k bananas from it; if the pile has fewer than k left, she finishes it and stops for that hour. Return the minimum integer k such that she can eat all the bananas within h hours. Constraints: 1 <= piles.length <= 10^4, piles.length <= h <= 10^9, 1 <= piles[i] <= 10^9.💡 Core Algorithmic Approaches
- This is a binary search on the answer: the predicate "can finish within
hhours with speedk" is monotone ink. - For a given
k, the hours needed aresum(ceil(p / k)). - The feasible range is
[1, max(piles)]; binary search for the smallest feasiblek. - Use
r = mid(notmid - 1) when the predicate holds, becausemiditself may be the answer.
💻 Benchmark Python3 Implementation
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
lo, hi = 1, max(piles)
while lo < hi:
mid = (lo + hi) // 2
# hours needed at speed mid; ceil division via integer arithmetic
hours = sum((p + mid - 1) // mid for p in piles)
if hours <= h:
hi = mid # mid works -> try smaller
else:
lo = mid + 1
return lo⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log(max(piles))): each predicate evaluation is O(n) and the search does O(log V) of them.
💾 Space Complexity
O(1): a few scalars.
⚠️ Interview Pitfalls & Follow-ups
- Using
hi = mid - 1when feasible: that could skip the optimal value. Usehi = mid. - Computing hours with
p // mid: integer division truncates; the ceiling(p + mid - 1) // midis required. - Using floating-point ceiling:
math.ceil(p / mid)loses precision for large values; the integer form is exact. - Setting
hi = max(piles)wrongly: the maximum useful speed ismax(piles), since eating faster than the largest pile wastes the hour's capacity. - Forgetting that
h >= piles.length: this guaranteesk = max(piles)is always feasible, so the search never fails.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Binary Search Problems
中文速览与面试要点
爱吃香蕉的珂珂
珂珂喜欢吃香蕉。这里有 n 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 h 小时后回来。珂珂可以决定她吃香蕉的速度 k(单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 k 根。如果这堆香蕉少于 k 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。返回她可以在 h 小时内吃掉所有香蕉的 最小速度 k(k 为整数)。约束:1 ≤ piles.length ≤ 10^4;piles.length ≤ h ≤ 10^9;1 ≤ piles[i] ≤ 10^9。
核心要点
- 二分答案(标准解) :本题不是「在数组中二分」,而是「在答案空间二分」——速度 k 的范围是 [1, max(piles)] 。
- 单调性(二分的前提) :k 越大,所需总时间越少。因此「能在 h 小时内吃完」关于 k 具有单调性(k 越大越容易满足),可以二分。
- 判定函数 feasible(k) :总时间 = Σ ceil(piles[i] / k) 。用整数运算写作 (p + k − 1) // k 。
- 二分模板 :找 最小 满足条件的 k,用「左闭右闭 + r = mid 」的形式。
常见陷阱
- 用浮点除法 math.ceil(p / k) :会引入浮点误差,且速度慢。用整数运算 (p + k − 1) // k 。
- 下界写成 0 :速度不能为 0(会除零)。下界应为 1。
- 上界写成 sum(piles) :虽然正确但二分次数更多。上界取 max(piles) 更紧(速度超过最大堆后不再有收益)。
- 二分模板用错方向 :本题求「最小可行值」,应写 if feasible: r = mid else: l = mid + 1 。若写成 l = mid 会死循环。
- 判定函数中把「吃不完的余数」算成 0 小时 :即使 p 不是 k 的整数倍,也需要完整的 1 小时,因此必须向上取整。
参考实现
class Solution:
def minEatingSpeed(self, piles: list[int], h: int) -> int:
def hours_needed(k: int) -> int:
# 每堆需要 ceil(p / k) 小时
return sum((p + k - 1) // k for p in piles)
l, r = 1, max(piles) # 速度的上下界
while l < r:
mid = (l + r) // 2
if hours_needed(mid) <= h:
r = mid # 可行 -> 尝试更小的速度
else:
l = mid + 1 # 不可行 -> 必须加速
return l