Leetcode—768. 最多能完成排序的块 II【困难】

2024每日刷题(149)

Leetcode---768. 最多能完成排序的块 II

实现代码

cpp 复制代码
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        // 分块
        // A | B
        // A 块中最大值肯定要小于 B 块中最小值!才能分块
        int ans = 1;

        int n = arr.size();
        vector<int> maxL(n);
        vector<int> minR(n);

        for(int i = 0; i < n; i++) {
            maxL[i] = i == 0 ? arr[0]: max(maxL[i - 1], arr[i]);
        }

        for(int i = n - 1; i >= 0; i--) {
            minR[i] = i == n - 1 ? arr[n - 1]: min(minR[i + 1], arr[i]);
        }

        // 在这里如果arr有n个元素, 那么最多切n-1块, 所以ans初始值是1, 即末尾最后一刀
        for(int i = 0; i < n - 1; i++) {
            if(maxL[i] <= minR[i + 1]) {
                ans++;
            }
        }
        return ans;
    }
};

运行结果


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
我命由我12345几秒前
Photoshop - Photoshop 使用仿制图章工具移除对象
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·学习方法
哼べ要伱管7 分钟前
26秋季上册-广东省电子基础性作业(小学)
经验分享·学习
All for pursuit.19 分钟前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
ZhangJun9522 分钟前
Mobike 共享单车分析项目
人工智能·python·算法·kmeans·聚类·knn
富唯智能机器人26 分钟前
仓储自动化改造,复合机器人降本利器|富唯智能
经验分享
Navigator_Z29 分钟前
LeetCode //C - 1281. Subtract the Product and Sum of Digits of an Integer
c语言·算法·leetcode
薛定e的猫咪38 分钟前
(AISTATS 2023)BaCaDI 逐章阅读
人工智能·深度学习·算法·机器学习
Benny_Tang1 小时前
AT_arc180_d [ARC180D] Division into 3 题解
数据结构·c++·算法
懒人81 小时前
Windows11系统优化指南
windows·经验分享
殷色玫瑰1 小时前
C++ STL:stack、queue、priority_queue 与容器适配器详解
java·开发语言·数据结构·c++·算法·visualstudio