Skip to content

Commit d3e4d25

Browse files
Remove-Covered-Intervals.cpp (#163)
* improved c++ code * Improved code with O(nlogn) solution Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 50b5fb1 commit d3e4d25

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

C++/Remove-Covered-Intervals.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,32 @@ class Solution {
2727
};
2828

2929
//Complexity: O(n*n)
30+
31+
32+
33+
SIMPLIFIED SOLUTION in O(nlogn) time
34+
35+
//
36+
class Solution {
37+
public:
38+
int removeCoveredIntervals(vector<vector<int>>& intervals) {
39+
sort(intervals.begin(),intervals.end(),[](vector<int>& v1,vector<int>& v2){
40+
if(v1[0]==v2[0])
41+
return v1[1]>v2[1];
42+
return v1[0]<v2[0];
43+
});
44+
int end = intervals[0][1];
45+
int sz = 1;
46+
for(int i=1;i<intervals.size();i++)
47+
{
48+
if(intervals[i][1]>end)
49+
{
50+
end = intervals[i][1];
51+
sz++;
52+
}
53+
}
54+
return sz;
55+
}
56+
};
57+
58+
//

0 commit comments

Comments
 (0)