[LeetCode] Max Chunks To Make Sorted

769. Max Chunks To Make Sorted

You are given an integer array arr of length n that represents a permutation of the integers in the range [0, n - 1].

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
int res = 0, ma = -1;
for(int i = 0; i < arr.size(); i++) {
ma = max(ma, arr[i]);
if(i == ma)
res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/21/PS/LeetCode/max-chunks-to-make-sorted/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.