The Question
CodingMax Chunks To Make Sorted II
Given an array of integers
arr (which may contain duplicates), we want to split the array into a number of contiguous chunks such that if we sort each chunk individually and concatenate them, the resulting array is sorted in non-decreasing order. Your goal is to find the maximum number of chunks that can be created to satisfy this condition.
Example:
Input: arr = [2, 1, 3, 4, 4]
Output: 4
Explanation: We can split into [2, 1], [3], [4], [4]. Sorting each gives [1, 2], [3], [4], [4], which concatenated is [1, 2, 3, 4, 4].
Constraints:
- 1 <= arr.length <= 10^5
- 0 <= arr[i] <= 10^8Swift
Prefix Sum
Suffix Min