#768

Max Chunks To Make Sorted II

candidate master · 1300 · lc hard +32 · verified · 54.7% accepted · 1,998 likes · top 47%

Description

Given an integer array arr, partition it into the maximum number of contiguous chunks such that individually sorting each chunk and concatenating the results yields the fully sorted array. Return the maximum number of chunks.

Example 1:

Input: arr = [5,4,3,2,1]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.

Example 2:

Input: arr = [2,1,3,4,4]
Output: 4
Explanation:
We can split into two chunks, such as [2, 1], [3, 4, 4].
However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.

Code

1
2
3