1288. 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.

 

Constraints:

  • 1 <= intervals.length <= 1000
  • 0 <= intervals[i][0] < intervals[i][1] <= 10^5
  • intervals[i] != intervals[j] for all i != j
class Solution {
    public int removeCoveredIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        int res = 0, left = -1, right = -1;
        for(int[] i: intervals){
            if(i[0] > left && i[1] > right){
                res++;
                left = i[0];
            }
            right = Math.max(right, i[1]);
        }
        return res;
    }
}

 

 https://leetcode.com/problems/remove-covered-intervals/discuss/451277/JavaC%2B%2BPython-Sort

remove covered intervals,remove完了呢?剩下的都是uncovered的,那uncovered的有什么性质呢?必须是完全不包裹,左大于左,右大于右。

这种题一般都会进行按起点排序,排好序后从前往后,如果是uncovered就res++,然后更新left和right,否则有三种情况1.左边重合(更新right),2. 右边重合(不用更新),3. 左右都重合(不用更新)。

posted @ 2020-01-18 01:29  Schwifty  阅读(299)  评论(0)    收藏  举报