Skip to content

Commit bd9fe12

Browse files
author
whd
committed
upload
1 parent a0c061f commit bd9fe12

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

435 Non-overlapping Intervals .cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for an interval.
3+
* struct Interval {
4+
* int start;
5+
* int end;
6+
* Interval() : start(0), end(0) {}
7+
* Interval(int s, int e) : start(s), end(e) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int eraseOverlapIntervals(vector<Interval>& intervals) {
13+
sort(begin(intervals), end(intervals), [] (const Interval &a, const Interval &b) {return a.end < b.end;});
14+
int last = -0x7fffffff - 1;
15+
int ans = 0;
16+
for (auto &interval : intervals) {
17+
if (interval.start >= last) {
18+
last = interval.end;
19+
ans++;
20+
}
21+
}
22+
return intervals.size() - ans;
23+
}
24+
};

0 commit comments

Comments
 (0)