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 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。