Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Max Chunks To Make Sorted - With Duplicates



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Maximum Number of Sorted Segments

Maximum Number of Sorted Segments

Problem Statement

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.

Examples

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.

Constraints

  • The length of nums is between 1 and 12 inclusive.
  • nums is a permutation of the integers from 0 to nums.length - 1.