Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
set-intersection-size-at-least-two.cpp 904 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-12-31 18:35 +08:00 . Create set-intersection-size-at-least-two.cpp
// Time: O(nlogn)
// Space: O(n)
// greedy solution
class Solution {
public:
int intersectionSizeTwo(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(),
[](const vector<int>& a, const vector<int>& b) {
return (a[0] != b[0]) ? (a[0] < b[0]) : (b[1] < a[1]);
});
vector<int> cnts(intervals.size(), 2);
int result = 0;
while (!intervals.empty()) {
auto start = intervals.back()[0]; intervals.pop_back();
auto cnt = cnts.back(); cnts.pop_back();
for (int s = start; s < start + cnt; ++s) {
for (int i = 0; i < intervals.size(); ++i) {
if (cnts[i] && s <= intervals[i][1]) {
--cnts[i];
}
}
}
result += cnt;
}
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

搜索帮助