All problems
0965MediumArrayStackGreedySortingMonotonic Stack

Most Blocks a Line Can Be Cut Into

Tracked in this browser only
Write code

Trains the technique from

LeetCode 769Max Chunks To Make Sorted

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 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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def max_chunks_to_sorted(arr: list[int]) -> int:
Java
public int maxChunksToSorted(int[] arr)
September 7
Apply