All problems
0017MediumArrayBinary Search

Overnight Print Rate

Tracked in this browser only
Write code

Trains the technique from

LeetCode 875Koko Eating Bananas

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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