-
Notifications
You must be signed in to change notification settings - Fork 481
/
0435.cpp
31 lines (29 loc) · 857 Bytes
/
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals)
{
if (intervals.empty()) return 0;
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){
if (a[0] != b[0]) return a[0] < b[0];
return a[1] < b[1];
});
int res = 0;
auto pre = intervals.begin();
for (auto cur = intervals.begin() + 1; cur != intervals.end(); cur++)
{
if ((*pre)[1] > (*cur)[0])
{
++res;
if ((*cur)[1] < (*pre)[1]) pre = cur;
}
else pre = cur;
}
return res;
}
};