Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
max-chunks-to-make-sorted-ii.cpp 558 Bytes
一键复制 编辑 原始数据 按行查看 历史
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
vector<int> idxs(arr.size());
iota(idxs.begin(), idxs.end(), 0);
sort(idxs.begin(), idxs.end(),
[&](int i1, int i2) {
return arr[i1] != arr[i2] ? arr[i1] < arr[i2] : i1 < i2;
});
int result = 0;
for (auto i = 0, max_i = 0; i < idxs.size(); ++i) {
max_i = max(max_i, idxs[i]);
if (max_i == i) ++result;
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助