-
Notifications
You must be signed in to change notification settings - Fork 5
/
day-187.cpp
65 lines (47 loc) · 1.39 KB
/
day-187.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Remove Covered Intervals
Given a list of intervals, remove all intervals that are covered by another
interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique.
*/
// Simple slow solution
class Solution {
public:
int removeCoveredIntervals(vector<vector<int>>& intervals) {
int answer = intervals.size();
for (int idx = 0; idx < intervals.size(); idx++) {
for (int jdx = 0; jdx < intervals.size(); jdx++) {
if (idx == jdx) continue;
vector<int> first = intervals[idx];
vector<int> current = intervals[jdx];
if (current[0] <= first[0] && current[1] >= first[1]) {
answer -= 1;
break;
}
}
}
return answer;
}
};