https://leetcode.com/contest/weekly-contest-68/problems/max-chunks-to-make-sorted-ver-1/
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
vector<int> sorted(arr);
sort(sorted.begin(), sorted.end());
vector<int> max;
int max_now = -1;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] > max_now) {
max_now = arr[i];
max.push_back(arr[i]);
}
else
max.push_back(max_now);
}
int total = 0;
for (int i = 0; i < arr.size(); i++) {
if (sorted[i] == max[i]) {
total++;
}
}
return total;
}
};
留言
張貼留言