贪心策略---不重叠的区间个数

不重叠的区间个数

435. Non-overlapping Intervals (Medium)

Input: [ [1,2], [1,2], [1,2] ]

Output: 2

Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
Input: [ [1,2], [2,3] ]

Output: 0

Explanation: You don't need to remove any of the intervals since they're already non-overlapping.

题目描述:

  计算让一组区间不重叠所需要移除的区间个数。

思路分析:

  先计算最多能组成的不重叠区间个数,然后用区间总数减去不重叠区间的个数。在每次选择中,区间的结尾最为重要,选择的区间结尾越小,留给后面的区间的空间越大,那么后面能够选择的区间个数也就越大。按照区间结尾进行排序,每次选择结尾最小。

代码:

public int eraseOverlapIntervals(Interval[]intervals){
    if(intervals==null||intervals.length==0)
        return 0;
    Arrays.sort(intervals,new Comparator<Interval>(){
        @Override
        public int compare(Interval o1,Interval o2){
            return o1.end-o2.end;
        }
    });
    int res=1;
    int end=intervals[0].end;
    for(int i=1;i<intervals.length;i++){
        if(intervals[i].start<end){
            continue;
        }
        cnt++;
        end=intervals[i].end;
    }
    return intervals.length-cnt;
}
上一篇:Javascript setTimeout函数开始执行太多次


下一篇:[POJ-1201]Intervals