Leetcode Insert Interval

Source: Internet
Author: User

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

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] .

Thinking Analysis: The problem itself is not difficult, but still debugging for a long time. Define the result Res container, first the newinterval start and intervals inside the end of the interval, find the position should be inserted newinterval, insert newinterval a little attention, NewInterval should be added first (may need to update its start before adding) to the res, and then continue to check later to see if the added NewInterval is still in conflict with the following interval, if there is a conflict, Update the end of NewInterval one after the merge interval (condition is newinterval.end >= intervals.get (i). Start). In Java, the list container holds objects newinterval the reference (address) of the , so the modification to NewInterval is also equivalent to the res modification. This backward check until the merge all can merge interval, and finally add the remaining interval to Res. Both time complexity and spatial complexity are O (N). Implementation, you can use a pointer i through the intervals to the end, for different segments for different processing. This question and leetcode Merge intervals have relations, can relate to think.

AC Code

/** * Definition for an interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0;} * Interval (int s, I NT e) {start = s; end = e;} *} */public class Solution {public list<interval> insert (list<interval> int        Ervals, Interval newinterval) {//1120 list<interval> res = new arraylist<interval> ();            if (intervals.size () = = 0) {res.add (newinterval);        return res;        } int i = 0; while (I < intervals.size () && newinterval.start > Intervals.get (i). End) {Res.add (Intervals.get (i)            );        i++;        } if (I < intervals.size ()) Newinterval.start = Math.min (Newinterval.start, Intervals.get (i). Start);        Res.add (NewInterval); First add NewInterval, then use NewInterval to merge the afterwards intervals that within the range//since we MA Ntain reference of NewInterval in res, the modification OF NewInterval also modify res//merge afterwards intervals one by one while (I < intervals.size () &&am P Newinterval.end >= Intervals.get (i). Start) {newinterval.end = Math.max (Newinterval.end, Intervals.get (i). end            );        i++;        } for (int j = i; J < Intervals.size (); j + +) {Res.add (Intervals.get (j));        } return res; 1139}}


Leetcode Insert Interval

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.