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.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

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;
    }
}

 1288. Remove Covered Intervals 随笔

 

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

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄