768. Max Chunks To Make Sorted II

Irish Mocha·2024년 1월 23일

Algorithm PS

목록 보기
3/9

Problem Description
https://leetcode.com/problems/max-chunks-to-make-sorted-ii/

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int n = arr.size();
        vector<int> left_max(n, 0);
        
        left_max[0] = arr[0];
        for (int i = 1; i < n; i++) {
            left_max[i] = max(left_max[i - 1], arr[i]);
        }
        
        vector<int> right_min(n, 0);
        right_min[n - 1] = arr[n - 1];
        for (int i = n - 2; i >= 0; --i) {
            right_min[i] = min(right_min[i + 1], arr[i]);
        }
        
        int answer = 0;
        for (int i = 0; i < n - 1; i++) {
            if (left_max[i] <= right_min[i + 1]) {
                ++answer;
            }
        }
        
        return ++answer;
    }
};
profile
irishmocha

0개의 댓글