提交 fe036776 编写于 作者: T tianzhongwei 提交者: labuladong

Update 贪心算法之区间调度问题.md

将添加的代码移动到了末尾
上级 652b36c6
......@@ -122,8 +122,62 @@ int findMinArrowShots(int[][] intvs) {
如果本文对你有帮助,欢迎关注我的公众号 labuladong,致力于把算法问题讲清楚~
[renxiaoyao](https://github.com/tianzhongwei) 提供C++解法代码:435题 无重叠区间
```C++
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
int n = intervals.size();
if(n <= 1) return 0;
auto myCmp = [&](const auto& a,const auto& b) {
return a[1] < b[1];
};
sort(intervals.begin(),intervals.end(),myCmp);
int cnt = 1;
int end = intervals[0][1]; // 区间动态历史最小值
for(const auto interval : intervals) {
int start = interval[0];
if(start >= end) {
cnt++;
end = interval[1];
}
}
return n - cnt;
}
};
```
[renxiaoyao](https://github.com/tianzhongwei) 提供C++解法代码:312 题 戳气球
```
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
int n = points.size();
if(n < 2) return n;
auto myCmp = [&](const auto& a,const auto& b) {
return a[1] < b[1];
};
sort(points.begin(),points.end(),myCmp);
int cnt = 1;
int end = points[0][1];
for(const auto& point : points) {
int start = point[0];
if(start > end) { // 若当前区间的起点在当前历史最右边界的后面
cnt++; // 则非重叠区间个数累加一
end = point[1]; // 更新当前历史最优边界
}
}
return cnt; // 返回非重叠区间的个数
}
};
```
[上一篇:动态规划之博弈问题](../动态规划系列/动态规划之博弈问题.md)
[下一篇:动态规划之KMP字符匹配算法](../动态规划系列/动态规划之KMP字符匹配算法.md)
[目录](../README.md#目录)
\ No newline at end of file
[目录](../README.md#目录)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册