zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    【linux基础】linux系统日志设置相关记录
    【linux基础】mount: unknown filesystem type 'exfat'
    [c++]float assign
    第6章 移动语义和enable_if:6.1 完美转发
    第5章 技巧性基础:5.7 模板模板参数
    第5章 技巧性基础:5.6 变量模板
    第5章 技巧性基础:5.4 原生数组和字符串字面量的模板
    第5章 技巧性基础:5.3 this->的使用
    第5章 技巧性基础:5.2 零初始化
    第5章 技巧性基础:5.1 关键字typename
  • 原文地址:https://www.cnblogs.com/freeneng/p/3213557.html
Copyright © 2011-2022 走看看