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]
.
Hide TagsArray Sort The problem is given a set of intervals, and then there is an interval, which merges the overlapping intervals and outputs the combined interval. Idea 1: The simple idea is to sequence the traversal, add the new interval to the sort, and then merge the overlapping, which has been written before. Idea 2: Train of thought 1 merging will only be done once, so change one, because a set of intervals is ordered, so you can try to find the combined head, tail, so that can accelerate. In order to quickly locate a merged kinsoku, you can use a binary search. I have defined a binary search, which is used to find the IDX of the maximum interval header that is not greater than target in a set of intervals, by header comparison. This gives the approximate positioning and then returns the part of a set of sections, then creates a new interval, joins the return, and finally adds the remaining back sections back. It is important to note that the creation interval may not be merged with others, that is, the added interval does not overlap with the other intervals, and it is troublesome to determine where to cut.
#include <vector>#include<iostream>using namespacestd;/** * Definition for an interval.*/structInterval {intstart; intend; Interval (): Start (0), End (0) {} Interval (intSinte): Start (s), End (e) {}};classSolution { Public: Vector<Interval> Insert (vector<interval> &intervals, Interval newinterval) { intn =intervals.size (); if(n<1)returnVector<interval>{{newinterval.start,newinterval.end}}; Vector<Interval>ret;//if (n==1) {//if (newinterval.end<intervals[0].start) {//Ret.push_back (NewInterval); Ret.push_back (Intervals[0]);//return ret;// }//if (intervals[0].end<newinterval.start) {//Ret.push_back (intervals[0]); Ret.push_back (newinterval);//return ret;// }// } intIDX1 =Binsearch (Intervals,newinterval.start); intIDX2 =Binsearch (intervals,newinterval.end); intIdx_tmp,newstart; if(intervals[idx1].end>=Newinterval.start) {idx_tmp= idx1-1; Newstart= Intervals[idx1].start<newinterval.start?Intervals[idx1].start:newinterval.start; } Else{idx_tmp=idx1; Newstart=Newinterval.start; } for(intI=0; i<=idx_tmp;i++) Ret.push_back (Intervals[i]); intNewend; if(newinterval.end<Intervals[idx2].start) {Newend=Newinterval.end; Idx_tmp=idx2; } Else{newend= Intervals[idx2].end>newinterval.end?Intervals[idx2].end:newinterval.end; Idx_tmp= idx2+1; } ret.push_back (Interval (newstart,newend)); for(inti=idx_tmp;i<n;i++) Ret.push_back (Intervals[i]); returnret; } intBinsearch (vector<interval> &ints,inttarget) { if(ints.size () = =1)return 0; intLFT =0, RGT = Ints.size ()-1; if(Ints[rgt].start<=target)returnRGT; if(Ints[lft].start>=target)returnLfT; intRET =0; Do{ intMid = (LFT+RGT)/2; if(Ints[mid].start>target) rgt=mid; Elselft=mid; RET=LfT; if(ints[ret+1].start>target) Break; } while(lft+1<RGT); returnret; }};intmain () {vector<Interval> intervals{{1,2},{3,5},{6,7},{8,Ten},{ A, -}};//vector<interval> intervals{{1,2}};//cout<<intervals[0].start<< "" <<intervals[1].end<<endl;Interval NewInterval (-1,0); Solution Sol; Vector<Interval> ret =Sol.insert (Intervals,newinterval); for(intI=0; I<ret.size (); i++) cout<<ret[i].start<<" "<<ret[i].end<<Endl; return 0;}
View Code
[Leetcode] Insert Interval Two-point search