class Solution
{
enum {START, END};
public:
//O(n log n) solution: sort + 1 pass
int removeCoveredIntervals(vector<vector<int>>& intervals)
{
std::sort(begin(intervals), end(intervals),
[](auto a, auto b)
{
return a[END] == b[END] ? a[START] > b[START] : a[END] < b[END];
}
);
//copy data to list for O(1) erasure
std::list<vector<int>> i_vals(begin(intervals), end(intervals));
//ENDs is already sorted, so next element has end >= than prev
//only thing is to check the start of the interval, if prev has START > next start
//then it's covered
for(auto iter = begin(i_vals); iter != prev(end(i_vals));)
{
if((*iter)[START] >= (*next(iter))[START])
{
iter = i_vals.erase(iter);
if(iter != begin(i_vals))
advance(iter, -1);
}
else
++iter;
}
return i_vals.size();
}
};