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;
    }
};

留言

這個網誌中的熱門文章

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/longest-univalue-path/

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/maximum-sum-of-3-non-overlapping-subarrays/