Max Chunks To Make Sorted II
Real-world version: Cutting the Row Into the Most BlocksThe statement here is our own: the same inputs, outputs and technique.
A row holds the weights weights, and weights may repeat. Cut the row into blocks of neighbouring crates so that sorting each block on its own leaves the whole row in non-decreasing order.
Return the largest number of blocks such a cut can use.
Examples
Example 1
- Input
- weights = [1, 3, 2, 4]
- Output
- 3
Cut after the first crate and after the third. The middle block of 3 and 2 sorts into 2 and 3, and the whole row comes out in order.
Example 2
- Input
- weights = [2, 3, 1, 4]
- Output
- 2
The 1 has to end up before both the 2 and the 3, so those three crates must share a block. Cutting after them gives two blocks.
Example 3
- Input
- weights = [1, 1]
- Output
- 2
Both crates weigh the same, so the cut between them is allowed and each is its own block.
Constraints
- 1 <= weights.length <= 2000
- 0 <= weights[i] <= 10^8
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.
def max_chunks_to_sorted(weights: list[int]) -> int:public int maxChunksToSorted(int[] weights)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Shortest Unsorted Continuous SubarrayReal-world version: Shortest Stretch to Re-sortMedium
- Max Chunks To Make SortedReal-world version: Most Blocks a Line Can Be Cut IntoMedium
- Car FleetReal-world version: Rafts Reaching the Canal LockMedium
- Minimum Number of Increments on Subarrays to Form a Target ArrayReal-world version: Sealant Runs Along a TowpathHard