-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_covered_intervals.cpp
More file actions
50 lines (48 loc) · 1.39 KB
/
remove_covered_intervals.cpp
File metadata and controls
50 lines (48 loc) · 1.39 KB
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
/*
* =====================================================================================
*
* Filename: remove_covered_intervals.cpp
*
* Description: 1288. Remove Covered Intervals
* https://leetcode.com/problems/remove-covered-intervals/
*
* Version: 1.0
* Created: 02/21/2022 11:04:54
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <climits>
#include <vector>
class Solution {
public:
int removeCoveredIntervals(std::vector<std::vector<int>>& intervals) {
int res = 0;
int left = INT_MIN;
int right = INT_MIN;
std::sort(intervals.begin(), intervals.end());
for (const std::vector<int>& cur : intervals) {
// (l1, r1), (l2, r2), (l3, r3)
// case 1: l1 == l2, overlapping
// case 2: l1 < l2 && l1 > l2, overlapping
// case 3: l1 < l2 && l1 < l2, not overlapping
if (cur[0] > left && cur[1] > right) {
left = cur[0];
res++;
}
right = std::max(right, cur[1]);
}
return res;
}
};
int main(int argc, char* argv[]) {
std::vector<std::vector<int>> intervals = {{1, 4}, {3, 6}, {2, 8}};
const int res = Solution().removeCoveredIntervals(intervals);
printf("%d\n", res);
return 0;
}