Leetcode insert Interval

Source: Internet
Author: User

Question: given a series of intervals, These intervals are not overlapped and sorted by the starting point of each interval. Another interval. How to obtain all merged intervals.

Example 1:
Given intervals[1,3],[6,9], Insert and merge[2,5]In[1,5],[6,9].

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

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

You can do it with thoughtful consideration. I think like this:

1. If the start of a given interval to be inserted is greater than the end of some previous intervals, those intervals must be part of the answer. Finally, you need to determine whether it is the end. If so, you can return the result after inserting the interval.

2. after 1, the end of the current interval is found to be greater or equal than the start value of the inserted interval. At this time, there must be a merge of the intervals. Then, we first determine the start value of the TMP interval after the merge. This start should be min (the start of the current interval and the start to be inserted). After the TMP end is found, the merging of the repetition interval is completed.

3. find the end and keep looking for it. If you know that finding an end is larger than the end to be inserted, you must check whether the end to be inserted is in the specified range, as shown in, the TMP end is the end that is traversed now. Otherwise, the TMP end is the end to be accessed.

4. Remember to record the remaining range (if any) after finding the end.

/*** Definition for an interval. * struct interval {* int start; * int end; * interval (): Start (0), end (0) {} * interval (int s, int e ): start (s), end (e) {}*}; */class solution {public: vector <interval> insert (vector <interval> & intervals, interval newinterval) {vector <interval> ans; vector <interval>: iterator P = intervals. begin (); interval TMP; while (P! = Intervals. end () & (* P ). end <newinterval. start) ans. push_back (* P ++); // locate the range with a large start point of newinterval. If there is any, output the following answer directly if (P = intervals. end () {ans. push_back (newinterval); Return ans;} TMP = * P; // TMP is mainly used to record the start and end points of the span if (* P ). start> newinterval. start) TMP. start = newinterval. start; // determine the start point while (P! = Intervals. end () & (* P ). end <newinterval. end) P ++; If (P = intervals. end () {TMP. end = newinterval. end; ans. push_back (TMP); Return ans;} If (* P ). start <= newinterval. end) // if the end is within the range, determine the end of TMP and record the remaining range to the answer to output {TMP. end = (* P ). end; ans. push_back (TMP); While (++ p! = Intervals. end () ans. push_back (* P); Return ans;} TMP. end = newinterval. end; ans. push_back (TMP); // The end is not in the interval, and the end is the end of TMP. Then, record the remaining interval while (P! = Intervals. End () ans. push_back (* P ++); Return ans ;}};

This solution is concise. His idea is to find out whether the end of the interval to be inserted is greater than the start of a certain interval, and I use the end.

Paste it directly.

vector<Interval> insert(vector<Interval> &v, Interval nv)       {          vector<Interval> rs;          int i = 0;          for ( ; i < v.size() && v[i].end < nv.start; i++)          {              rs.push_back(v[i]);          }          if (i == v.size())          {              rs.push_back(nv);              return rs;          }          nv.start = min(nv.start, v[i].start);            for ( ; i < v.size() && v[i].start <= nv.end; i++)          {              nv.end = max(nv.end, v[i].end);          }          rs.push_back(nv);            for ( ; i < v.size(); i++)          {              rs.push_back(v[i]);          }          return rs;      }  

 

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.