美文网首页
435. Non-overlapping Intervals

435. Non-overlapping Intervals

作者: ryderchan | 来源:发表于2017-04-01 09:30 被阅读66次

题目如下:

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note:

  1. You may assume the interval's end point is always bigger than its start point.
  2. Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.

我的提交:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
 
 /*
 *@Author:chen
 *time:o(nlogn),Arrays.sort使用的是归并排序
 *先排序,后查找重叠任务删除。其实就是安排任务,只是换了种说法。
 */
import java.util.Arrays;
public class Solution {
    public int eraseOverlapIntervals(Interval[] intervals) {
        Arrays.sort(intervals,(o1,o2)->o1.end==o2.end?o2.start-o1.start:o1.end-o2.end);
        int currentEnd = Integer.MIN_VALUE,removeNum = 0;
        for(int i=0;i<intervals.length;i++){
            if(intervals[i].start>=currentEnd)
                currentEnd = intervals[i].end;
            else
                removeNum++;
        }
        return removeNum;
    }
}

相关文章

网友评论

      本文标题:435. Non-overlapping Intervals

      本文链接:https://www.haomeiwen.com/subject/ybtiottx.html