forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0435.cpp
38 lines (37 loc) · 1.01 KB
/
0435.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//Definition for an interval.
struct Interval
{
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int eraseOverlapIntervals(vector<Interval>& intervals)
{
if (intervals.empty()) return 0;
sort(intervals.begin(), intervals.end(), [](const Interval& i1, const Interval& i2){
if (i1.start != i2.start) return i1.start < i2.start;
return i1.end < i2.end;
});
int result = 0;
auto prev = intervals.begin();
for (auto cur = intervals.begin() + 1; cur != intervals.end(); ++cur)
{
if (prev->end > cur->start)
{
++result;
if (cur->end < prev->end) prev = cur;
}
else prev = cur;
}
return result;
}
};