成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 435. Non-overlapping Intervals

mrcode / 2590人閱讀

Problem

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.

Solution
class Solution {
    public int eraseOverlapIntervals(Interval[] intervals) {
        if (intervals == null || intervals.length == 0) return 0;
        //find non-overlapping count, so sort by end
        Arrays.sort(intervals, (a, b)->(a.end-b.end));
        int end = intervals[0].end;
        int count = 1;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i].start >= end) {
                end = intervals[i].end;
                count++;
            }
        }
        return intervals.length-count;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72267.html

相關(guān)文章

  • [LeetCode] Insert Interval

    Problem Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times....

    Jonathan Shieber 評(píng)論0 收藏0
  • [LeetCode] Employee Free Time

    Problem We are given a list schedule of employees, which represents the working time for each employee. Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order. ...

    go4it 評(píng)論0 收藏0
  • leetcode--57--Insert Interval

    摘要:?jiǎn)栴}描述分析這道題的關(guān)鍵在于理解問題,抽取原型,理解中間可以部分如何界定,以及非部分如何進(jìn)行追加。需要注意的是循環(huán)到最后一個(gè)元素和在最后一個(gè)元素的區(qū)別。 問題描述: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You m...

    kycool 評(píng)論0 收藏0
  • [Leetcode] Merge Intervals and Insert Interval 合并間

    摘要:我們只要把所有和該有重疊的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表連起來,就是結(jié)果了。 Merge Intervals 最新更新請(qǐng)見 https://yanjia.me/zh/2019/02/... Given a collection of intervals, merge all overlapping intervals.For example, Gi...

    antyiwei 評(píng)論0 收藏0
  • leetcode57. Insert Interval

    摘要:題目要求給定一組順序排列且相互之間沒有重疊的區(qū)間,輸入一個(gè)區(qū)間,將它插入到當(dāng)前的區(qū)間數(shù)組中,并且將需要合并的區(qū)間合并,之后返回插入并且合并后的區(qū)間。我們將這三個(gè)類型的區(qū)間分別標(biāo)注為類型,類型,類型。 題目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge...

    Yuanf 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<