All problems
0650HardArrayMathGreedy

Cutting Crates Into a Rising Manifest

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2366Minimum Replacements to Sort the Array

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 freight manifest lists crate weights in loading order as weights. The loader will only accept the manifest when the weights never decrease as you read it from the first entry to the last.

One cut takes a single crate of weight w and replaces it with two crates whose weights are positive whole numbers adding up to w. The two new crates take the cut crate's place in the manifest, one after the other, so the manifest gets one entry longer. Either of the new crates may be cut again later.

Return the least number of cuts that makes the manifest never decrease.

Examples

Example 1

Input
weights = [12, 8, 6]
Output
3

Cut the 8 into 4 and 4, then cut the 12 into 4 and 8 and cut that 8 into 4 and 4. The manifest now reads 4, 4, 4, 4, 4, 6, which never decreases, after three cuts.

Example 2

Input
weights = [10, 3, 3]
Output
3

Three cuts turn the 10 into 2, 2, 3 and 3, leaving the manifest reading 2, 2, 3, 3, 3, 3, which never decreases.

Example 3

Input
weights = [2, 4, 6]
Output
0

The manifest already never decreases, so no cut is needed.

Constraints

  • 1 <= weights.length <= 10^5
  • 1 <= weights[i] <= 10^9
  • The answer is at most 10^15 for every input allowed above.

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_crate_splits(weights: list[int]) -> int:
Java
public long minCrateSplits(int[] weights)
September 7
Apply