[Leetcode 86] 57 Insert Interval

Problem:

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

Analysis:

The basic way is to go through the intervals and compared each of them with the new interval. Two situations happens:

1. They intersect. For intersection cases, just generate a new interval that merging the former two together and continue to go through the vector.

2. They don't intersect. There are two sub-situations here:

  a. The i-th interval in the vector has a start time larger than the merged interval's end time. For this case, it means we find the place to put the newly merged interval. Push it      and then push all the remaining intervals into the result vector.

  b. The i-th interval in the vectoe has an end time less than the merged interval's start time. For this case, we need to push the i-th interval into the result and check the next      interval in the vector.

3. Some special cases to be considered are that the newInterval includes the whole vector, is the last one in the result vector etc.

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  */
10 class Solution {
11 public:
12     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
13         // Start typing your C/C++ solution below
14         // DO NOT write int main() function
15         vector<Interval> res;
16         
17         if (intervals.size() == 0) {
18             res.push_back(newInterval);
19             return res;
20         }
21         
22         for (int i = 0; i < intervals.size(); i++) {
23             if (intersect(newInterval, intervals[i])) {
24                 newInterval = merge(newInterval, intervals[i]);
25                 if (i == intervals.size()-1)
26                     res.push_back(newInterval);
27             }
28             else if (newInterval.start > intervals[i].end) {
29                 res.push_back(intervals[i]);
30                 if (i == intervals.size() - 1)
31                     res.push_back(newInterval);
32             } 
33             else if (newInterval.end < intervals[i].start) {
34                 res.push_back(newInterval);
35                 for (int j=i; j<intervals.size(); j++)
36                     res.push_back(intervals[j]);
37                     
38                 break;
39             }
40             
41         }
42         
43         return res;
44     }
45     
46     bool intersect(const Interval &a, const Interval &b) {
47         return !((a.start > b.end) || (b.start > a.end));
48     }
49     
50     Interval merge(const Interval &a, const Interval &b) {
51         Interval res;
52         res.start = min(a.start, b.start);
53         res.end = max(a.end, b.end);
54         return res;
55     }
56     
57     int min(const int &a, const int &b) { return (a < b) ? a : b; }
58     
59     int max(const int &a, const int &b) { return (a > b) ? a : b; }
60 };
View Code
原文地址:https://www.cnblogs.com/freeneng/p/3213557.html