leetcode435:无重叠区间

无重叠区间

给定一个区间的集合 intervals ,其中 intervals[i] = [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠

 public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals,(o1,o2) -> {
            return o1[0] - o2[0];
        });
        int count = 0;
        int cover = intervals[0][1];
        for (int i = 1; i < intervals.length;i++) {
            if(intervals[i][0] < cover){
                count++;
                if(cover >= intervals[i][1]){
                    cover = intervals[i][1];
                }
            }else{
                cover = intervals[i][1];
            }
        }
        return count;
    }

贪心,先排序,再删除

你可能感兴趣的:(leetcode,java)