码迷,mamicode.com
首页 > 移动开发 > 详细

Leetcode: Non-overlapping Intervals

时间:2016-12-07 07:35:26      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:bsp   最大   int start   put   like   ret   tco   sum   []   

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.
Example 1:
Input: [ [1,2], [2,3], [3,4], [1,3] ]

Output: 1

Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
Example 2:
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.
Example 3:
Input: [ [1,2], [2,3] ]

Output: 0

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

Actually, the problem is the same as "Given a collection of intervals, find the maximum number of intervals that are non-overlapping." (the classic Greedy problem: Interval Scheduling). With the solution to that problem, guess how do we get the minimum number of intervals to remove? : )

Sorting Interval.end in ascending order is O(nlogn), then traverse intervals array to get the maximum number of non-overlapping intervals is O(n). Total is O(nlogn).

开始的时候想岔了,以为是要求同一时刻overlap的最多interval数,但仔细想一想就发现不对,应该是non-overlap的interval的最大数目

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public int eraseOverlapIntervals(Interval[] intervals) {
12         if (intervals.length == 0) return 0;
13         int nonOverlap = 1;
14         int seq = 0;
15         Arrays.sort(intervals, new Comparator<Interval>() {
16             public int compare(Interval i1, Interval i2) {
17                 return i1.end - i2.end;
18             }
19         });
20         for (int i=1; i<intervals.length; i++) {
21             if (intervals[i].start >= intervals[seq].end) {
22                 seq = i;
23                 nonOverlap++;
24             }
25         }
26         return intervals.length - nonOverlap;
27     }
28 }

 

Leetcode: Non-overlapping Intervals

标签:bsp   最大   int start   put   like   ret   tco   sum   []   

原文地址:http://www.cnblogs.com/EdwardLiu/p/6139762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!