Leetcode Insert Interval-----java

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

This problem is similar to the one above, that is, given a number of sorted arrays and a separate array, insert a separate array into that array collection, and merge the related collections.

The first thing to write is to find the place where begin insertion begins and where the insertion ends last, and then delete and merge, but the results are not ideal.

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { Publiclist<interval> Insert (list<interval>intervals, Interval newinterval) {                intStart = Newinterval.start,end = Newinterval.end,len =intervals.size (); intBegin = 0, last = len-1; if(len = = 0) {intervals.add (newinterval); returnintervals; }         while(Begin<len && intervals.get (BEGIN). End <start) Begin++; if(Begin = =Len)            {Intervals.add (newinterval); returnintervals; }         while(Last>=0 && intervals.get (last). Start >end) last--; //From the begin to the last        if(Begin >Last )            {Intervals.add (begin, NewInterval); returnintervals; } Interval ans=NewInterval (); Ans.start= Intervals.get (begin). Start>newinterval.start?newInterval.start:intervals.get (Begin). Start; Ans.end= Intervals.get (last). End>newinterval.end?Intervals.get (last). End:newInterval.end;        Intervals.set (begin, ANS);  for(inti = begin+1;i<=last;i++) {Intervals.remove (Begin+1); }                        returnintervals; }    }

And then made a little change, that last did not start from the end, but from the beginning of the place, the result is slower = = ...

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { Publiclist<interval> Insert (list<interval>intervals, Interval newinterval) {        intStart = Newinterval.start, end = Newinterval.end, Len =intervals.size (); intBegin = 0, last = 0 ; if(len = = 0) {intervals.add (newinterval); returnintervals; }        if(End < Intervals.get (0). Start) {Intervals.add (0, NewInterval); returnintervals; }        if(Start > Intervals.get (len-1). End) {Intervals.add (newinterval); returnintervals; }         while(Start >intervals.get (BEGIN). end) Begin++; Last=begin;  while(Last < Len && end >=Intervals.get (last). Start) last++; if(Begin = =Last )            {Intervals.add (begin,newinterval); returnintervals; } Start= Intervals.get (begin). Start>start?start:intervals.get (Begin). Start; End= Intervals.get (last-1). End>end?intervals.get (last-1). End:end;  for(inti = 1;i<last-begin;i++) intervals.remove (begin); Intervals.get (Begin). Start=start; Intervals.get (BEGIN). End=end; returnintervals; }}

An estimate of this low efficiency should be caused by the remove operation, because each remove operation is removed after the element is deleted, and then all elements are moved forward once, so that the efficiency is very low.

So change the way the direct remove, instead, first move the back of the collection, and then remove the collection from the back. The efficiency is high.

/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { Publiclist<interval> Insert (list<interval>intervals, Interval newinterval) {        intStart = Newinterval.start, end = Newinterval.end, Len =intervals.size (); intBegin = 0, last = 0 ; if(len = = 0) {intervals.add (newinterval); returnintervals; }        if(End < Intervals.get (0). Start) {Intervals.add (0, NewInterval); returnintervals; }        if(Start > Intervals.get (len-1). End) {Intervals.add (newinterval); returnintervals; }         while(Start >intervals.get (BEGIN). end) Begin++; Last=begin;  while(Last < Len && end >=Intervals.get (last). Start) last++; if(Begin = =Last )            {Intervals.add (begin,newinterval); returnintervals; } intervals.get (Begin). Start= Intervals.get (begin). Start>start?start:intervals.get (Begin). Start; Intervals.get (BEGIN). End= Intervals.get (last-1). End>end?intervals.get (last-1). End:end;  for(inti = 0;i<len-last;i++) Intervals.set (Begin+1+i,intervals.get (last+i));  for(inti = 1;i<last-begin;i++) Intervals.remove (Len-i); returnintervals; }}

Leetcode Insert Interval-----java

Related Article

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.