国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

【LC總結(jié)】Intervals題目 (Insert/Merge/Number of Airplane

Achilles / 1369人閱讀

摘要:忘了這題怎么做,汗顏無地。邊界用記錄每個(gè)時(shí)刻的飛行數(shù)目。對(duì)于某一時(shí)刻,起飛和降落同時(shí)發(fā)生,只計(jì)算一次。先強(qiáng)勢(shì)插入,再

Merge Intervals Problem

Given a collection of intervals, merge all overlapping intervals.

Example

Given intervals => merged intervals:

[                     [
  [1, 3],               [1, 6],
  [2, 6],      =>       [8, 10],
  [8, 10],              [15, 18]
  [15, 18]            ]
]
Challenge

O(n log n) time and O(1) extra space.

Note

忘了這題怎么做,汗顏無地。

邊界: size < 2

sort by Comparator

loop: merge & removal

return

Solution
public class Interval {
    int start, end;
    Interval(int start, int end) {
        this.start = start;
        this.end = end;
    }
}
class Solution {
    public List merge(List intervals) {
        if (intervals.size() < 2) return intervals;
        Collections.sort(intervals, new Comparator() {
            public int compare(Interval l1, Interval l2) {
                return l1.start - l2.start;
            }
        });
        Interval pre = intervals.get(0);
        for (int i = 1; i < intervals.size(); i++) {
            Interval cur = intervals.get(i);
            if (cur.start <= pre.end) {
                pre.end = Math.max(pre.end, cur.end);
                intervals.remove(cur);
                i--;
            }
            else pre = cur;
        }
        return intervals;
    }
}
Number of Airplanes in the Sky Problem

Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?

Notice

If landing and flying happens at the same time, we consider landing should happen at first.

Example

For interval list

[
  [1,10],
  [2,3],
  [5,8],
  [4,7]
]

Return 3

Note

用HashMap記錄每個(gè)時(shí)刻的飛行數(shù)目。
對(duì)于某一時(shí)刻,起飛和降落同時(shí)發(fā)生,只計(jì)算一次。

Solution
class Solution {
    public int countOfAirplanes(List airplanes) { 
        Map map = new HashMap<>();
        int max = Integer.MIN_VALUE;
        if (airplanes == null || airplanes.size() == 0) return 0;
        for (Interval cur: airplanes) {
            for (int i = cur.start; i < cur.end; i++) {
                if (map.containsKey(i)) map.put(i, map.get(i)+1);
                else map.put(i, 1);
                max = Math.max(max, map.get(i));
            }
        }
        return max;
    }
}
Insert Interval Problem

Given a non-overlapping interval list which is sorted by start point.

Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).

Example

Insert [2, 5] into [[1,2], [5,9]], we get [[1,9]].

Insert [3, 4] into [[1,2], [5,9]], we get [[1,2], [3,4], [5,9]].

Note

先強(qiáng)勢(shì)插入,再merge

Solution
class Solution {
    public ArrayList insert(ArrayList intervals, Interval newInterval) {
        
        //check null condition;
        if (intervals == null || intervals.size() == 0) {
            if (newInterval != null) {
                intervals.add(newInterval);
            }
            return intervals;
        }
        
        //add newInterval in right position no matter if it"s overlapped;
        int start = newInterval.start;
        int pos = -1;
        for (int i = 0; i < intervals.size(); i++) {
            if (intervals.get(i).start <= start) {
                pos = i;
            }
        }
        intervals.add(pos+1, newInterval);
        
        //merge the intervals;
        Interval pre = intervals.get(0);
        Interval cur = pre;
        for (int i = 1; i < intervals.size(); i++) {
            cur = intervals.get(i);
            if (pre.end >= cur.start) {
                pre.end = pre.end > cur.end ? pre.end: cur.end;
                //.remove(i) followed by i-- to stay in this position after next loop i++
                intervals.remove(i);
                i--;
            }
            else pre = cur;
        }
        
        return intervals;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66052.html

相關(guān)文章

  • leetcode56 Merge Intervals

    摘要:題目要求輸入一系列區(qū)間,將出現(xiàn)交叉的區(qū)間合并起來,并將合并后的區(qū)間返回。將這兩個(gè)數(shù)組分別排序可以得到和。也就是說,合并后的結(jié)果和是等價(jià)的。舉個(gè)栗子,輸入為,先判斷是否是獨(dú)立區(qū)間,可見可以和合并,則將修改為。 題目要求 Given a collection of intervals, merge all overlapping intervals. For example, Given...

    shmily 評(píng)論0 收藏0
  • 352. Data Stream as Disjoint Intervals

    摘要:題目解答這題用會(huì)通過很快定位到當(dāng)前數(shù)所處在哪兩個(gè)之間,從而進(jìn)行高效的比較與合并。 題目:Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For exampl...

    maybe_009 評(píng)論0 收藏0
  • leetcode436. Find Right Interval

    摘要:題目要求假設(shè)一個(gè)二維的整數(shù)數(shù)組中每一行表示一個(gè)區(qū)間,每一行的第一個(gè)值表示區(qū)間的左邊界,第二個(gè)值表示區(qū)間的右邊界。 題目要求 Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal ...

    robin 評(píng)論0 收藏0
  • LC總結(jié)】Iterator題目<Zigzag 1&2><BST>&

    摘要:方法直接查找數(shù)組的位的迭代器,調(diào)用方法得到的整數(shù)即為要返回的元素。再寫迭代器的方法返回指針元素的并讓指針通過遞歸方法指向下一個(gè)元素。 Zigzag Iterator Problem Given two 1d vectors, implement an iterator to return their elements alternately. Example Given two 1d ...

    WelliJhon 評(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元查看
<