"Leetcode" "Hard" 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 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] .

Problem Solving Ideas:

1, first use two times to find and to be inserted in the interval has a relationship between the range;

2, will have the relationship of the interval to do processing;

3. Generate a new merged interval and return;

Problem Solving steps:

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: A     Static BOOLPartial_order (ConstInterval &a,ConstInterval &b) { -         returnA.end <B.start; -     }; the      -vector<interval> Insert (std::vector<interval> &intervals, Interval newinterval) { -Vector<interval>::iterator less =Lower_bound (Intervals.begin (), Intervals.end (), NewInterval, partial_order); -Vector<interval>::iterator greater =Upper_bound (Intervals.begin (), Intervals.end (), NewInterval, partial_order); +          -Vector<interval>answer; + Answer.insert (Answer.end (), Intervals.begin (), less); A         if(Less <Greater) { atNewinterval.start = Min (Newinterval.start, (*(less ). Start); -Newinterval.end = Max (Newinterval.end, (* (Greater-1) . end); -         } - Answer.push_back (newinterval); - Answer.insert (Answer.end (), Greater, intervals.end ()); -          in         returnanswer; -   } to};

Do not use Lower_bound and upper_bound to write the AC bad code, left for improvement:

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) { -         if(Intervals.empty ()) -             returnVector<interval> (1, newinterval); the         intleft =0; -         intright = Intervals.size ()-1; -         intSize =intervals.size (); -         intIns_start, Ins_end; +          -          while(Left <Right ) { +             intMid = (left + right)/2; A             if(Intervals[mid].end <Newinterval.start) atLeft = mid +1; -             Else  -right =mid; -         } -Ins_start =Left ; -          inleft = left = =0?0: Left-1; -right = Intervals.size ()-1; to          while(Left <Right ) { +             intMid = (left + right)/2+1; -             if(Newinterval.end <Intervals[mid].start) theright = mid-1; *             Else $left =mid;Panax Notoginseng         } -Ins_end =Right ; the          +         if(Ins_end = =0&& Newinterval.end < intervals[0].start) { A Intervals.insert (Intervals.begin (), newinterval); the             returnintervals; +         } -         if(Ins_start = = Size-1&& Newinterval.start > Intervals[size-1].end) { $ Intervals.push_back (newinterval); $             returnintervals; -         } -          theVector<interval>answer; -Answer.insert (Answer.end (), Intervals.begin (), Intervals.begin () +Ins_start);Wuyi         if(Ins_start <=ins_end) { theNewinterval.start =min (Newinterval.start, intervals[ins_start].start); -Newinterval.end =Max (newinterval.end, intervals[ins_end].end); Wu         } - Answer.push_back (newinterval); AboutAnswer.insert (Answer.end (), Intervals.begin () + Ins_end +1, Intervals.end ()); $         returnanswer; -          -     } -};

"Leetcode" "Hard" 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.