Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
russian-doll-envelopes.cpp 874 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-06-07 22:42 +08:00 . Update russian-doll-envelopes.cpp
// Time: O(nlogn + nlogk) = O(nlogn), k is the length of the result.
// Space: O(1)
class Solution {
public:
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
vector<int> result;
sort(envelopes.begin(), envelopes.end(), // O(nlogn)
[](const pair<int, int>& a, const pair<int, int>& b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
});
for (const auto& envelope : envelopes) {
const auto target = envelope.second;
auto it = lower_bound(result.begin(), result.end(), target); // O(logk)
if (it == result.end()) {
result.emplace_back(target);
} else {
*it = target;
}
}
return result.size();
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助