首页 > 其他 > 详细

1288. Remove Covered Intervals

时间:2020-01-18 09:31:06      阅读:76      评论:0      收藏:0      [点我收藏+]

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

1288. Remove Covered Intervals

原文:https://www.cnblogs.com/wentiliangkaihua/p/12208051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!