Given an array intervals where intervals[i] = [li, ri] represent the interval [li, ri), remove all intervals that are covered by another interval in the list.

The interval [a, b) is covered by the interval [c, d) if and only if c &lt;= a and b &lt;= d.

Return the number of remaining intervals.

&nbsp;
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


&nbsp;
Constraints:


	1 &lt;= intervals.length &lt;= 1000
	intervals[i].length == 2
	0 &lt;= li &lt; ri &lt;= 105
	All the given intervals are unique.


class Solution:
    def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:

        intervals.sort(key = lambda x: (x[0], -x[1]))
        current, count = intervals[0], 1
        for i in range(1, len(intervals)):
            if current[0] <= intervals[i][0] and intervals[i][1] <= current[1]:
                continue
            current = intervals[i]
            count += 1
        return count

# time and space complexity
# time: O(nlog(n))
# space: O(1)
class Solution {
    public int removeCoveredIntervals(int[][] intervals) {
        if(intervals == null || intervals.length == 0) return 0;
        Arrays.sort(intervals, (i1,i2) -> (i1[0]==i2[0]?i2[1]-i1[1]:i1[0]-i2[0]));
        int c = intervals[0][0], d = intervals[0][1];
        int ans = intervals.length;
        for(int i=1;i<intervals.length;i++){
            int a = intervals[i][0], b = intervals[i][1];
            if(c<=a && b<=d) ans--; 
            else {
                c = a;
                d = b;
            }
        }
        return ans;
    }
}
class Solution {
public:
    int removeCoveredIntervals(vector<vector<int>>& intervals) 
    {
        int cnt = 0, last = INT_MIN;
        sort(intervals.begin(), intervals.end(), 
             [] (const vector<int>& v1, const vector<int>& v2) { 
                 if(v1[0] != v2[0]) return v1[0] < v2[0];
                 else return v1[1] > v2[1];
        });

        for(int i=0; i<intervals.size(); i++)
        {
            if(intervals[i][1] <= last) ++cnt;
            
            if(intervals[i][1] > last) last = intervals[i][1];
        }
        
        return intervals.size()-cnt;
    }
}; 
var removeCoveredIntervals = function(intervals) {
  intervals.sort((a,b)=> a[0]-b[0] || b[1]-a[1]);
  let overlap=0;
  for (i=1,prev=0; i<intervals.length; i++)
//    if ((intervals[prev][0] <= intervals[i][0]) //no need to check, already done in sort
//      && (intervals[prev][1] >= intervals[i][1]))
	if (intervals[prev][1] >= intervals[i][1]) // just look at 2nd index
      overlap++  // add to skipped elements
    else
      prev=i; // didn't overlap, so we can advance our previous element
  return intervals.length-overlap;
};
Remove Covered Intervals