Max Chunks To Make Sorted

Medium Topics Company Tags

The array arr is a permutation of the integers from 0 through arr.length - 1. Split it into nonempty contiguous chunks. Sort each chunk separately, then concatenate the chunks in their original order. Return the greatest number of chunks for which the final array is sorted in increasing order.

Example 1:

Input: arr = [0,2,1,3]

Output: 3

Explanation: Use the chunks [0], [2,1], and [3]. Sorting them gives [0], [1,2], and [3], which concatenate to [0,1,2,3]. Splitting the middle chunk would leave 2 before 1.

Example 2:

Input: arr = [2,0,1]

Output: 1

Explanation: The value 2 must move past both smaller values, so all three values must stay in one chunk.

Example 3:

Input: arr = [0,1,2]

Output: 3

Explanation: Every singleton chunk is already sorted. The three chunks [0], [1], and [2] achieve the maximum possible count, 3.

Constraints:

  • 1 <= arr.length <= 10
  • 0 <= arr[i] < arr.length
  • All values in arr are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.