All problems
0636MediumArrayBinary Search

Smallest Tallest Stack After Splits

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1760Minimum Limit of Balls in a Bag

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 depot floor holds several stacks of cartons. stacks[i] is the number of cartons in stack i.

One split takes a single stack off the floor and puts back two stacks in its place, each holding at least one carton, together holding exactly what the original stack held. A stack put back may be split again later. You are allowed at most splits splits, and you may also use fewer, or none at all.

The peak of the floor is the number of cartons in its tallest stack. Return the smallest peak you can leave behind.

Examples

Example 1

Input
stacks = [9, 9], splits = 2
Output
5

Split each stack of 9 into 5 and 4, using both allowed splits. The floor then holds 5, 4, 5 and 4 cartons, so its peak is 5.

Example 2

Input
stacks = [4, 4], splits = 1
Output
4

One split is allowed. Splitting a stack of 4 into 2 and 2 leaves the floor holding 2, 2 and 4 cartons, whose peak is 4. Carrying out no split at all also leaves a peak of 4.

Example 3

Input
stacks = [1, 1, 1], splits = 2
Output
1

Every stack already holds a single carton and a split has to leave at least one carton on each side, so no split is possible and the peak is 1.

Constraints

  • 1 <= stacks.length <= 10^5
  • 1 <= splits <= 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 minimum_size(stacks: list[int], splits: int) -> int:
Java
public int minimumSize(int[] stacks, int splits)
September 7
Apply