Trains the technique from
LeetCode 769Max Chunks To Make SortedThis 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 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.
Example 1
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
Each neighbouring pair is swapped, so the line cuts into three blocks of two.
Example 3
The whole line is reversed, so no cut works and it must be sorted as one block.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def max_chunks_to_sorted(arr: list[int]) -> int:public int maxChunksToSorted(int[] arr)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.