Non-overlapping intervals / Merge Interval

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.

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.

思路
思路與merge interval一致

  1. 通過Comparator對intervals進行排序,先按照區間的start升序,如果start相等再按照end升序排序
  2. 如果后一個區間與前一個區間重合,那么說明后一區間需要去掉才能保證不重合,此時count+1.
  3. 如果前后2個區間不重合,那么更新前一區間的起始范圍,繼續判斷。
/**
 * 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; }
 * }
 */
class Solution {
    public int eraseOverlapIntervals(Interval[] intervals) {
        int count = 0;
        if (intervals == null || intervals.length == 0) {
            return count;
        }
        
        Arrays.sort(intervals, (o1, o2) -> o1.start == o2.start? o1.start - o2.start : o1.end - o2.end);
        
        Interval pre = intervals[0];
        
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i].start < pre.end) {
               // pre.end = Math.max(pre.end, intervals[i].end);
                count++;
                continue;
            } else {
                pre = intervals[i];   
            }
        }
        return count;
    }
}

Merge Intervals

思路
  1. (考點)用comparator自定義sort的方式
  2. 設置第0個元素為last interval,
  3. 從intervals的第1個元素開始遍歷intervals Array,每次為curInterval.
    • 比較,curInterval.start >= lastInterval.last : 更新last interval的范圍,此時并不向result中加入該更新的interval,因為后面后可能還有需要merge到當前interval的元素
    • 如,cur 與 last無重合:result.add(last); last = curInterval
  4. !!!記得把最后一個last加入到結果中
/**
 * Definition of Interval:
 * public class Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 */


public class Solution {
    /*
     * @param intervals: interval list.
     * @return: A new interval list.
     */
    public List<Interval> merge(List<Interval> intervals) {
        // write your code here
        // 1. !!!用comparator自定義sort的方式(考點)
        // 2. 設置第0個元素為last interval,
        // 3. 從intervals的第1個元素開始遍歷intervals Array,每次為curInterval
        // 比較,curInterval.start >= lastInterval.last =》 更新last interval的范圍,此時并不向result中加入該更新的interval,因為后面后可能還有需要merge到當前interval的元素
        // 如,cur 與 last無重合:result.add(last); last = curInterval
        // 4. !!!記得把最后一個last加入到結果中
        
        List<Interval> result = new ArrayList<Interval>();
        
        if (intervals == null || intervals.size() < 1) {
            return result;
        }
        
        //1. Sort intervals based on start
        Collections.sort(intervals, new intervalComparator());
        
        //2. merge
        Interval lastInt = intervals.get(0);
        
        for (int i = 1; i < intervals.size(); i++) {
            Interval curInt = intervals.get(i);
            if (curInt.start <= lastInt.end) { //last的end可能比cur的end更大,或小
                lastInt.end = Math.max(lastInt.end, curInt.end); 
            } else {
                result.add(lastInt);
                lastInt = curInt;
            }
        }
        
        //3. DONT forget the LAST lastInt
        result.add(lastInt);
        return result;
    }
}

//@override Comparator interface
class intervalComparator implements Comparator<Interval> {
    public int compare(Interval o1, Interval o2) {
        return o1.start - o2.start;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,769評論 0 33
  • 決定要寫手帳之后不知道會不會把簡書扔掉,但是感覺短期內應該不會,畢竟對手帳還不是特別熟悉,如果最近有什么想法肯定會...
    GoodLee閱讀 141評論 0 0
  • 注重精神食糧的大抵都單身抑或對生活現狀有更高的追求,他們往往都有過人之處,懂得自省,與自己的靈魂對話,不將就。 有...
    w尺素寸心閱讀 238評論 1 1
  • 老鷹來了 老鷹來了 媽媽大驚失色 擋在我身前 與命運苦苦斡旋 牽著媽媽的衣襟 與老鷹捉迷藏 忽進忽退,忽東忽西 就...
    文藝調頻閱讀 608評論 29 27
  • 上天安排了一部叫天差地別的宏篇長劇,在通往天堂與地獄之門的人間舞臺連續上演,翻來覆去永不劇終。參演的蕓蕓眾生,在天...
    金指尖的花園閱讀 225評論 0 1