Show problems as
Classic problem #1011MediumArrayBinary Search

Capacity To Ship Packages Within D Days

Real-world version: Kiln Load Ceiling
Tracked in this browser only
Read Capacity To Ship Packages Within D Days on LeetCode(opens in a new tab)

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

Learn the pattern firstRange Search · Binary Search, animated step by step

A ceramics studio has one kiln and a queue of unfired pieces. The queue is given as masses, where masses[i] is the clay mass in grams of the piece standing at position i. Pieces leave the queue from the front only, so they are fired in exactly the listed order and are never rearranged.

The studio has booked sessions kiln runs. A single run takes some number of pieces off the front of the queue, which means each run fires one contiguous block of the queue, and it may take none at all. The kiln is rated for a load ceiling: the combined mass loaded into any one run may not exceed it.

Return the smallest load ceiling for which sessions runs are enough to fire the entire queue. A run is never split across sessions, and a piece is never fired twice.

Examples

Example 1

Input
masses = [9, 4, 6, 3, 8], sessions = 3
Output
11

With a ceiling of 11 the runs can be 9 | 4, 6 | 3, 8. Dropping to 10 forces the trailing 3 and 8 apart, which needs a fourth run.

Example 2

Input
masses = [5, 5, 5, 5], sessions = 4
Output
5

Four runs are booked for four pieces, so each run can hold one piece and the ceiling only has to cover the heaviest piece.

Example 3

Input
masses = [2, 4, 6, 1], sessions = 1
Output
13

A single run must swallow the whole queue, so the ceiling equals the total mass of 13 grams.

Constraints

  • 1 <= sessions <= masses.length <= 5 * 10^4
  • 1 <= masses[i] <= 500

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 kiln_load_ceiling(masses: list[int], sessions: int) -> int:
Java
public int kilnLoadCeiling(int[] masses, int sessions)