768. Max Chunks To Make Sorted II
https://leetcode.com/problems/max-chunks-to-make-sorted-ii/
/*
* @lc app=leetcode id=768 lang=cpp
*
* [768] Max Chunks To Make Sorted II
*/
// @lc code=start
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
const int N = arr.size();
vector<int> expected(arr);
sort(expected.begin(), expected.end());
int res = 0;
long sum = 0, sum2 = 0;
for (int i = 0; i < N; ++i) {
sum += arr[i];
sum2 += expected[i];
if (sum == sum2) ++res;
}
return res;
}
};
// @lc code=end
Last updated