Max Chunks To Make Sorted II

You are given an integer array arr.

We split arr into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.

Return the largest number of chunks we can make to sort the array.

Example 1
Inputarr = [5,4,3,2,1]
Output1
Splitting into two or more chunks will not return the required result; for example, splitting into [5, 4], [3, 2, 1] results in [4, 5, 1, 2, 3], which is not sorted.
Example 2
Inputarr = [2,1,3,4,4]
Output4
Splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.

Constraints

  • 1 <= arr.length <= 2000
  • 0 <= arr[i] <= 10^8

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate