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 Partitions

Maximum Number of Sorted Partitions

Problem Statement

Given a list of integers nums which may contain duplicates, your task is to divide the list into several partitions (chunks), sort each chunk individually, and then concatenate all chunks. The concatenated list should be sorted in ascending order.

Determine the maximum number of such partitions that can be created.

Examples

Example 1:

Input:
nums = [4, 3, 2, 1, 5]
Output:
1
Explanation:
Splitting into multiple chunks like [4, 3], [2, 1, 5] results in [3, 4, 1, 2, 5], which is not sorted.
Therefore, only one chunk (the entire array) works.
    

Example 2:

Input:
nums = [1, 2, 3, 4, 4]
Output:
5
Explanation:
Each element can be a chunk by itself, since sorting each single-element chunk and concatenating results in the sorted list.
    

Example 3:

Input:
nums = [2, 1, 3, 1, 4]
Output:
3
Explanation:
One possible partition is [2, 1], [3, 1], [4]. After sorting each chunk and concatenating, the final list is sorted.
    

Constraints

  • The length of nums is between 1 and 2500 inclusive.
  • Each element in nums is an integer between 0 and 107 inclusive.