Problem
Ask
Submissions

Problem: Koko Eating Bananas

Medium
30 min
Explore how to apply a modified binary search to find the minimum constant eating speed for Koko to finish all banana piles within the specified time. This lesson helps you understand problem constraints, logical reasoning, and efficient implementation to solve search optimization problems in interviews.

Statement

Koko has nn piles of bananas in front of her, where the ithi^{th} pile has piles[i] bananas. The guards have left and will return in h hours, and Koko must finish all the bananas before they come back.

Before eating, Koko chooses an integer as an eating speed kk (bananas per hour). She keeps this speed constant throughout.

In each hour, Koko selects one pile of bananas and eats from it according to the following rules:

  • If the pile contains at least kk bananas, she eats exactly kk bananas from it.

  • If the pile contains fewer than kk bananas, she eats all the bananas from that pile and does not eat from any other pile during that hour.

Your task is to return the minimum eating speed kk that allows Koko to finish all the bananas within h hours before the guards return.

Constraints:

  • 11 \leq piles.length \leq 10310^{3}

  • piles.length \leqh \leq 10910^{9}

  • 11 \leqpiles[i] \leq 10410^{4}

Problem
Ask
Submissions

Problem: Koko Eating Bananas

Medium
30 min
Explore how to apply a modified binary search to find the minimum constant eating speed for Koko to finish all banana piles within the specified time. This lesson helps you understand problem constraints, logical reasoning, and efficient implementation to solve search optimization problems in interviews.

Statement

Koko has nn piles of bananas in front of her, where the ithi^{th} pile has piles[i] bananas. The guards have left and will return in h hours, and Koko must finish all the bananas before they come back.

Before eating, Koko chooses an integer as an eating speed kk (bananas per hour). She keeps this speed constant throughout.

In each hour, Koko selects one pile of bananas and eats from it according to the following rules:

  • If the pile contains at least kk bananas, she eats exactly kk bananas from it.

  • If the pile contains fewer than kk bananas, she eats all the bananas from that pile and does not eat from any other pile during that hour.

Your task is to return the minimum eating speed kk that allows Koko to finish all the bananas within h hours before the guards return.

Constraints:

  • 11 \leq piles.length \leq 10310^{3}

  • piles.length \leqh \leq 10910^{9}

  • 11 \leqpiles[i] \leq 10410^{4}