Max Chunks To Make Sorted
Real-world version: Most Blocks a Line Can Be Cut IntoThe statement here is our own: the same inputs, outputs and technique.
A line of labels is given as arr, holding each of the numbers from 0 up to one less than its length exactly once.
Cut the line into blocks of neighbouring labels, then sort each block on its own and join the blocks back up in the same order. Return the greatest number of blocks for which this leaves the whole line in increasing order.
Examples
Example 1
- Input
- arr = [2, 0, 1, 4, 3]
- Output
- 2
Cutting after position 2 and after position 4 gives the blocks 2, 0, 1 and 4, 3, each of which sorts into place. Cutting anywhere else would leave a label in the wrong block.
Example 2
- Input
- arr = [1, 0, 3, 2, 5, 4]
- Output
- 3
Each neighbouring pair is swapped, so the line cuts into three blocks of two.
Example 3
- Input
- arr = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
- Output
- 1
The whole line is reversed, so no cut works and it must be sorted as one block.
Constraints
- 1 <= arr.length <= 10
- 0 <= arr[i] <= arr.length - 1
- Every number from 0 to arr.length - 1 appears exactly once
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(arr: list[int]) -> int:public int maxChunksToSorted(int[] arr)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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 Sorted IIReal-world version: Cutting the Row Into the Most BlocksHard
- 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