-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
38 lines (35 loc) · 1.32 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <vector>
class Solution {
public:
int maxChunksToSorted(std::vector<int>& arr) {
// given permutation of intergers in the range [0..n-1]
// partition arr and sort each partition. Concatenating the sorted chunks
// together should return the sorted array {0, 1, ..., n-1}
// Some form of monotonic property.
// An already-sorted array can be splitted into n chunks, each of size 1.
// Therefore, any decreasing subarrays can not be splitted. Only increasing
// subarrays can.
// NOTE: Not exactly right either. Notice that if the largest element is
// at the front of the array, the entire array can only be splitted into
// a single chunk, regardless of the suffix.
// Hmmm. Monotonic stack doesnt seem simple. Seems to be something about
// the relative positions of the max elements?
//
// Hm. Given that the elements are distinct in the range of [0..n-1],
// notice that given a prefix subarray, all the elements within are
// sortable if it is less than the expected max of that prefix.
const std::size_t n = arr.size();
int max = 0;
int chunks = 0;
for (int i = 0; i < n; ++i) {
max = std::max(max, arr[i]);
if (max <= i) {
++chunks;
}
}
return chunks;
}
};