-
Notifications
You must be signed in to change notification settings - Fork 0
/
1288.remove-covered-intervals.cpp
50 lines (46 loc) · 1.15 KB
/
1288.remove-covered-intervals.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
39
40
41
42
43
44
45
46
47
48
49
50
/*
* @lc app=leetcode id=1288 lang=cpp
*
* [1288] Remove Covered Intervals
*/
// @lc code=start
class Solution
{
static bool comp(vector<int> &a, vector<int> &b)
{
if (a[0] != b[0])
return a[0] < b[0];
return a[1] > b[1]; // want to be longest first, so we can cover the following
}
public:
int removeCoveredIntervals(vector<vector<int>> &intervals)
{
// sort + pq
// 按首端点排序
// ##----
// ----
// than the beginning of the first interval is unique and cannot be covered
sort(intervals.begin(), intervals.end(), comp);
const int n = intervals.size();
int i = 0;
int cnt = 0;
while (i < n)
{
int j = i + 1;
cnt++;
while (j < n && intervals[i][1] >= intervals[j][1])
{
// interval[j] is covered by interval[i]
++j;
}
i = j;
}
return cnt;
}
};
// @lc code=end
// sorted by thae starting point
// => min num of intervals to cover the whole range
// [1,4], [1,2], [3,4]
// i
// j