题目如下:
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:
- You may assume the interval's end point is always bigger than its start point.
- 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;
}
}