Asked at

Koko Eating Bananas

Hard
Verified
ArrayBinary Search~30 min

Koko eats bananas from piles over h hours. Each hour she picks one pile and eats up to her speed k bananas; if the pile is smaller, she finishes it and waits out the hour. Return the smallest integer k that lets her finish all piles within h hours.

Binary search on the speed. The input arrives as a single object { piles, h }.

Examples

in{ piles: [3, 6, 7, 11], h: 8 }
out4

At speed 4, the piles take 1 + 2 + 2 + 3 = 8 hours.

in{ piles: [30, 11, 23, 4, 20], h: 5 }
out30

Five piles in five hours forces one pile per hour, so speed must cover the max.

Constraints

  • 1 ≤ piles.length ≤ 10⁴
  • piles.length ≤ h ≤ 10⁹
  • 1 ≤ piles[i] ≤ 10⁹
  • Target: O(n log m) time, where m is the largest pile.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.