Show problems as
Classic problem #875MediumArrayBinary Search

Koko Eating Bananas

Real-world version: Overnight Print Rate
Tracked in this browser only
Read Koko Eating Bananas on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstAllocation Problems · Binary Search, animated step by step

A copy shop leaves one press running through the night. stacks[i] is the number of sheets waiting in stack i, and the shift lasts hours hours.

The operator sets a single whole number rate before leaving, and it cannot be touched again. Once an hour begins, the press draws from exactly one stack and pulls at most rate sheets out of it. A stack holding fewer than rate sheets is simply emptied, and the press then sits idle until the hour is up instead of turning to another stack.

The operator would rather run the press gently, so return the smallest rate that still leaves every stack empty by the end of the shift.

The shift is never shorter than the number of stacks, so a workable rate always exists.

Examples

Example 1

Input
stacks = [15, 8, 5, 20], hours = 9
Output
7

At 7 sheets an hour the stacks take 3, 2, 1 and 3 hours, which is 9 in total. Dialling 6 pushes the first and last stacks to 3 and 4 hours, for 10 in total, one hour past the shift.

Example 2

Input
stacks = [20, 14, 9], hours = 3
Output
20

The shift allows exactly one hour per stack, so the press has to clear the biggest stack in a single hour and the rate cannot dip below 20.

Example 3

Input
stacks = [17], hours = 17
Output
1

A single stack of 17 sheets fits in 17 one-sheet hours, and no smaller whole rate exists.

Constraints

  • 1 <= stacks.length <= 10^4
  • stacks.length <= hours <= 10^9
  • 1 <= stacks[i] <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def min_print_rate(stacks: list[int], hours: int) -> int:
Java
public int minPrintRate(int[] stacks, int hours)