leetcode题解-57. Insert Interval

题目:

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.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

本题是对于给定的non-overlapping intervals和一个新的intervals,要将新的插进去并进行合并,然后我们可以想到无非就是分别找到newInterval.start和newInterval.end插入的位置即可。因此我们可以使用二插搜索的方法分别找到其茶如位置,然后将器进行合并,代码入下所示:

public List insert1(List intervals, Interval newInterval) {
        int n = intervals.size();
        if (n == 0) {
            intervals.add(newInterval);
            return intervals;
        }

        int low = 0, high = n - 1, mid = 0;
        int temp, target = newInterval.start;
        while (low <= high) {
            mid = (low + high) / 2;
            temp = intervals.get(mid).start;
            if (temp == target)
                break;
            if (temp < target)
                low = mid + 1;
            else
                high = mid - 1;
        }

        // insIdx = the index where new interval to be inserted
        int insIdx = (low <= high) ? mid : low;
        Interval pre = (insIdx == 0) ? null : intervals.get(insIdx - 1);
        // 0<=insIdx<=n, pre=[insIdx-1], pre.start1;
        target = newInterval.end;
        while (low <= high) {
            mid = (low + high) / 2;
            temp = intervals.get(mid).end;
            if (temp == target)
                break;
            if (temp < target)
                low = mid + 1;
            else
                high = mid - 1;
        }

        // nxtIdx= the next index after the inserted new interval
        int nxtIdx = (low <= high) ? mid : low;
        Interval nxt = (nxtIdx == n) ? null : intervals.get(nxtIdx);
        // insIdx<=nxtIdx<=n, nxt=[nxtIdx], nxt.end>=new.end

        // [0]...[insIdx-1] <--> [insIdx]...[nxtIdx-1][nxtIdx]...[n]
        intervals.subList(insIdx, nxtIdx).clear();

        // check whether newInterval can be merged with pre or nxt
        boolean isMerged = false, isMerged2 = false;
        if (insIdx > 0 && pre.end >= newInterval.start) {
            pre.end = Math.max(pre.end, newInterval.end);
            isMerged = true;
        }

        if (nxtIdx < n && newInterval.end >= nxt.start) {
            nxt.start = Math.min(nxt.start, newInterval.start);
            isMerged2 = isMerged;
            isMerged = true;
        }

        if (!isMerged) {
            intervals.add(insIdx, newInterval);
            return intervals;
        }

        // merged with pre or nxt or both, deal with the both case
        if (isMerged2 && pre.end >= nxt.start) {
            nxt.start = pre.start; // pre.start < new.start, nxt.start;
            intervals.remove(insIdx - 1); // remove pre
        }

        return intervals;
    }

另外一种思路就是我们遍历链表,将其分为三部分,第一部分是比newInterval.start小的,我们直接将其插入到结果之中,第二部分是需要合并的,我们生成一个新的Interval并插入,第三部分是后面的比end打的,我们也直接插入结果即可。代码入小所示:

    //53.6%
    public List insert(List intervals, Interval newInterval) {
        List result = new ArrayList<>();
        int i = 0;
        // add all the intervals ending before newInterval starts
        while (i < intervals.size() && intervals.get(i).end < newInterval.start)
            result.add(intervals.get(i++));
        // merge all overlapping intervals to one considering newInterval
        while (i < intervals.size() && intervals.get(i).start <= newInterval.end) {
            newInterval = new Interval( // we could mutate newInterval here also
                    Math.min(newInterval.start, intervals.get(i).start),
                    Math.max(newInterval.end, intervals.get(i).end));
            i++;
        }
        result.add(newInterval); // add the union of intervals we got
        // add all the rest
        while (i < intervals.size()) result.add(intervals.get(i++));
        return result;
    }

你可能感兴趣的:(leetcode刷题,leetcode)