Leetcode 57. Insert Interval

Source: Internet
Author: User

Insert Interval
    • Total accepted:64309
    • Total submissions:260619
    • Difficulty:hard

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 in as [2,5] [1,5],[6,9] .

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

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

Idea: Interval division. The left boundary of the new interval is found, and the right boundary of the new interval is found. Drawing will be clearer.

Left boundary: Not satisfying intervals[i].end<newinterval.start means intervals[i-1].end<newinterval.start<=intervals[i].end.

Right boundary: When Intervals[j].start<newinterval.end is satisfied, the interval is merged, Intervals[j] and NewInterval are merged into New NewInterval, and then try and intervals[j+ 1] Merge until it cannot be merged. Because Intervals[j] and intervals[j+1] are interval Independent, intervals[j] merging does not affect intervals[j+1].

Code:

1 /**2 * Definition for an interval.3 * struct Interval {4 * int start;5 * int end;6 * Interval (): Start (0), end (0) {}7 * Interval (int s, int e): Start (s), End (e) {}8  * };9  */Ten classSolution { One  Public: Avector<interval> Insert (vector<interval>&intervals, Interval newinterval) { -         inti,j; -Vector<interval>Res; the          for(i=0; I<intervals.size () &&intervals[i].end<newinterval.start;i++) {//I - Res.push_back (Intervals[i]); -         } -          for(J=i;j<intervals.size () &&intervals[j].start<=newinterval.end;j++){ +Newinterval=Interval (min (Intervals[j].start,newinterval.start), Max (intervals[j].end,newinterval.end)); -         } + Res.push_back (newinterval); A          for(; J<intervals.size (); j + +){ at Res.push_back (Intervals[j]); -         } -         returnRes; -     } -};

Leetcode 57. 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.