Max Chunks To Make Sorted - With Duplicates
Given a list nums
which contains all integers from 0 to nums.length - 1
in some order, you want to divide this list into several "segments". Each segment is then sorted individually, and after concatenating all sorted segments, the final list should be sorted in ascending order.
Your task is to find the greatest number of segments you can split the list into while still achieving the described property.
Example 1: Input: nums = [3, 2, 1, 0, 4] Output: 1 Explanation: If you try to split into multiple segments, such as [3, 2], [1, 0, 4], sorting them individually and concatenating won't give the fully sorted list. Therefore, only one segment (the whole list) works here.
Example 2: Input: nums = [0, 1, 2, 4, 3] Output: 4 Explanation: One possible segmentation is [0], [1], [2], [4, 3]. Sorting each segment individually and concatenating results in [0, 1, 2, 3, 4], which is sorted. This is the maximum number of segments achievable.
nums
is between 1 and 12 inclusive.nums
is a permutation of the integers from 0 to nums.length - 1
.