zoukankan      html  css  js  c++  java
  • LeetCode

    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 };
  • 相关阅读:
    在Windows环境下搭建redis
    三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较
    ASP.NET Web API身份验证和授权
    quartz 设置时间格式
    服务端发post请求产生的编码问题
    大型网站的灵魂——性能
    大型网站系统架构的演化
    c# url自动解码解决方案
    C# RSA非对称加密实现
    .net上传图片之使用第三方平台七牛上传图片接口
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3547417.html
Copyright © 2011-2022 走看看