Skip to main content
Back to problems
Leetcode
Medium
Arrays
Binary Search
Math
Amazon
Koko Eating Bananas

Find the minimum integer eating speed that lets Koko finish all banana piles within hh hours.

Acceptance 0%
Problem Statement

Problem

Koko has several piles of bananas. In each hour, she chooses one pile and eats bananas from that pile at a fixed integer speed kk bananas per hour.

If a pile has fewer than kk bananas, she eats the whole pile in that hour. Koko cannot split an hour across multiple piles.

Given the array of pile sizes and a limit of hh hours, determine the minimum integer speed kk such that Koko can finish all bananas within hh hours.

Goal

Return the smallest positive integer speed that makes the total time required less than or equal to hh.

Input Format

  • An integer array piles, where piles[i] is the number of bananas in the ii-th pile.
  • An integer h, the maximum number of hours available.

Output Format

  • Return a single integer: the minimum eating speed k.

Constraints

  • 1 <= piles.length
  • 1 <= piles[i]
  • 1 <= h
  • The answer is guaranteed to fit in a 32-bit signed integer.

Notes

  • For a given speed k, the required hours for one pile is pilek\lceil \frac{pile}{k} \rceil.
  • The total time is the sum over all piles.
Examples
Sample cases returned by the problem API.

Example 1

Input

piles = [3, 6, 7, 11], h = 8

Output

4

Explanation

At speed 4, the time needed is 3/4+6/4+7/4+11/4=1+2+2+3=8\lceil 3/4 \rceil + \lceil 6/4 \rceil + \lceil 7/4 \rceil + \lceil 11/4 \rceil = 1 + 2 + 2 + 3 = 8. No smaller speed can finish in 8 hours.

Example 2

Input

piles = [30, 11, 23, 4, 20], h = 5

Output

30

Explanation

Koko must finish one pile per hour, so she needs a speed large enough to eat the largest pile in one hour. The minimum such speed is 30.

Show 1 more example

Example 3

Input

piles = [30, 11, 23, 4, 20], h = 6

Output

23

Explanation

At speed 23, the hours are 2+1+1+1+1=62 + 1 + 1 + 1 + 1 = 6. Any smaller speed requires more than 6 hours.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.