769.最多能完成排序的块
链接:769.最多能完成排序的块
难度:Medium
标签:栈、贪心、数组、排序、单调栈
简介:返回数组能分成的最多块数量。
题解 1 - cpp
- 编辑时间:2022-10-13
- 执行用时:4ms
- 内存消耗:7MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int n = arr.size(), nmax = arr[0], ans = 0;
        for (int i = 0; i < n; i++) {
            nmax = max(nmax, arr[i]);
            if (nmax == i) ans++;
        }
        return ans;
    }
};