Insert Interval

2014.2.13 01:23

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

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

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

Solution:

  For the new interval, find out where the start and end value lies in at the old intervals. The search process can be linear or binary, as the intervals are sorted. But writing binary search for intervals won't be as easy as for conventional array.

  When the positions of start and end are found, insert the new interval accordingly. I don't think words will be an easy way to explain things here. Maybe you can draw some example on your draft and figure it out.

  Segment tree may be a bit overqualified for this job, but it's purely the insert operation. You insert a new interval and adjust the tree.

  Total time complexity is O(n). Space complexity is O(1).

Accepted code:

  1 // 1WA, 1AC, O(n) solution.
  2 /**
  3  * Definition for an interval.
  4  * struct Interval {
  5  *     int start;
  6  *     int end;
  7  *     Interval() : start(0), end(0) {}
  8  *     Interval(int s, int e) : start(s), end(e) {}
  9  * };
 10  */
 11 class Solution {
 12 public:
 13     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
 14         int i, n;
 15         int in1, in2;
 16         int i1, i2;
 17         int ll, rr;
 18         vector<Interval> res;
 19         
 20         n = (int)intervals.size();
 21         if (n == 0) {
 22             res.push_back(newInterval);
 23             return res;
 24         }
 25         
 26         ll = newInterval.start;
 27         rr = newInterval.end;
 28 
 29         if (ll < intervals[0].start) {
 30             i1 = -1;
 31             in1 = 0;
 32         } else if (ll > intervals[n - 1].end) {
 33             i1 = n - 1;
 34             in1 = 0;
 35         } else {
 36             for (i = 0; i < n - 1; ++i) {
 37                 if (ll >= intervals[i].start && ll <= intervals[i].end) {
 38                     // inside the interval
 39                     i1 = i;
 40                     in1 = 1;
 41                     break;
 42                 } else if (ll > intervals[i].end && ll < intervals[i + 1].start) {
 43                     i1 = i;
 44                     in1 = 0;
 45                     break;
 46                 }
 47             }
 48             if (i == n - 1) {
 49                 i1 = i;
 50                 in1 = 1;
 51             }
 52         }
 53 
 54         if (rr < intervals[0].start) {
 55             i2 = -1;
 56             in2 = 0;
 57         } else if (rr > intervals[n - 1].end) {
 58             i2 = n - 1;
 59             in2 = 0;
 60         } else {
 61             for (i = 0; i < n - 1; ++i) {
 62                 if (rr >= intervals[i].start && rr <= intervals[i].end) {
 63                     // inside the interval
 64                     i2 = i;
 65                     in2 = 1;
 66                     break;
 67                 } else if (rr > intervals[i].end && rr < intervals[i + 1].start) {
 68                     i2 = i;
 69                     in2 = 0;
 70                     break;
 71                 }
 72             }
 73             if (i == n - 1) {
 74                 i2 = i;
 75                 in2 = 1;
 76             }
 77         }
 78         
 79         for (i = 0; i < i1; ++i) {
 80             res.push_back(intervals[i]);
 81         }
 82         
 83         if (i1 == -1) {
 84             ll = newInterval.start;
 85         } else if (in1 == 1) {
 86             ll = intervals[i1].start;
 87         } else {
 88             res.push_back(intervals[i1]);
 89             ll = newInterval.start;
 90         }
 91         if (i2 == -1) {
 92             rr = newInterval.end;
 93         } else if (in2 == 1) {
 94             rr = intervals[i2].end;
 95         } else {
 96             rr = newInterval.end;
 97         }
 98         res.push_back(Interval(ll, rr));
 99         
100         for (i = i2 + 1; i < n; ++i) {
101             res.push_back(intervals[i]);
102         }
103         
104         return res;
105     }
106 };

 

 posted on 2014-02-13 02:27  zhuli19901106  阅读(232)  评论(0编辑  收藏  举报