File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ using interval = vector<int >; // length 2
2
+ // [1, 3] and [1, 19]
3
+ // [1, 19] can contain [1, 3]
4
+ bool comparator (const interval &left, const interval &right) {
5
+ if (left[0 ] == right[0 ])
6
+ return left[1 ] > right[1 ];
7
+ return left[0 ] < right[0 ]; // asc order of starting time
8
+ }
9
+
10
+ class Solution {
11
+ public:
12
+ int removeCoveredIntervals (vector<interval>& intervals) {
13
+ sort (intervals.begin (), intervals.end (), comparator);
14
+ int ans = intervals.size ();
15
+
16
+ int maxEnd = 0 ;
17
+
18
+ for (interval cur: intervals) {
19
+ // need to check if cur is already contained
20
+ int curEnd = cur[1 ];
21
+ if (curEnd <= maxEnd) {
22
+ ans--; // throw away the current interval
23
+ }
24
+
25
+ maxEnd = max (maxEnd, curEnd);
26
+ }
27
+
28
+ return ans;
29
+ }
30
+ };
You can’t perform that action at this time.
0 commit comments