File tree Expand file tree Collapse file tree 1 file changed +55
-1
lines changed
Expand file tree Collapse file tree 1 file changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -122,8 +122,62 @@ int findMinArrowShots(int[][] intvs) {
122122
123123如果本文对你有帮助,欢迎关注我的公众号 labuladong,致力于把算法问题讲清楚~
124124
125+ [ renxiaoyao] ( https://github.com/tianzhongwei ) 提供C++解法代码:435题 无重叠区间
126+ ``` C++
127+ class Solution {
128+ public:
129+ int eraseOverlapIntervals(vector<vector<int >>& intervals) {
130+ int n = intervals.size();
131+ if(n <= 1) return 0;
132+ auto myCmp = [ &] (const auto& a,const auto& b) {
133+ return a[ 1] < b[ 1] ;
134+ };
135+ sort(intervals.begin(),intervals.end(),myCmp);
136+ int cnt = 1;
137+ int end = intervals[ 0] [ 1 ] ; // 区间动态历史最小值
138+ for(const auto interval : intervals) {
139+ int start = interval[ 0] ;
140+ if(start >= end) {
141+ cnt++;
142+ end = interval[ 1] ;
143+ }
144+ }
145+ return n - cnt;
146+ }
147+ };
148+ ```
149+
150+
151+ [renxiaoyao](https://github.com/tianzhongwei) 提供C++解法代码:312 题 戳气球
152+ ```
153+ class Solution {
154+ public:
155+ int findMinArrowShots(vector<vector<int >>& points) {
156+ int n = points.size();
157+ if(n < 2) return n;
158+
159+ auto myCmp = [ &] (const auto& a,const auto& b) {
160+ return a[ 1] < b[ 1] ;
161+ };
162+ sort(points.begin(),points.end(),myCmp);
163+
164+ int cnt = 1;
165+ int end = points[ 0] [ 1 ] ;
166+ for(const auto& point : points) {
167+ int start = point[ 0] ;
168+ if(start > end) { // 若当前区间的起点在当前历史最右边界的后面
169+ cnt++; // 则非重叠区间个数累加一
170+ end = point[ 1] ; // 更新当前历史最优边界
171+ }
172+ }
173+ return cnt; // 返回非重叠区间的个数
174+ }
175+ };
176+ ```
177+
178+
125179[上一篇:动态规划之博弈问题](../动态规划系列/动态规划之博弈问题.md)
126180
127181[下一篇:动态规划之KMP字符匹配算法](../动态规划系列/动态规划之KMP字符匹配算法.md)
128182
129- [ 目录] ( ../README.md#目录 )
183+ [目录](../README.md#目录)
You can’t perform that action at this time.
0 commit comments