Skip to content

Commit

Permalink
Update 贪心算法之区间调度问题.md
Browse files Browse the repository at this point in the history
将添加的代码移动到了末尾
  • Loading branch information
tianzhongwei authored and labuladong committed Jun 23, 2020
1 parent 652b36c commit fe03677
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion 动态规划系列/贪心算法之区间调度问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#目录)
[目录](../README.md#目录)

0 comments on commit fe03677

Please sign in to comment.