博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Insert Interval
阅读量:6319 次
发布时间:2019-06-22

本文共 3996 字,大约阅读时间需要 13 分钟。

By far the best solution I have seen is of O(n) time (some solutions claim to be of O(logn) turns out to be O(n)). One of the simplest ideas is to compare each interval in intervals (intervals[i]) with newInterval and then perform respective operations according to their relationships.

  1. If they overlap, merge them to newInterval;
  2. If intervals[i] is to the left of newInterval, push intervals[i] to the result vector;
  3. If newInterval is to the left of intervals[i], push newInterval and all the remaining intervals (intervals[i], ..., intervals[n - 1]) to the result vector.

The code is as follows.

1 class Solution { 2 public: 3     vector
insert(vector
& intervals, Interval newInterval) { 4 vector
res; 5 int n = intervals.size(); 6 for (int i = 0; i < n; i++) { 7 if (intervals[i].end < newInterval.start) 8 res.push_back(intervals[i]); 9 else if (newInterval.end < intervals[i].start) {10 res.push_back(newInterval);11 for (int j = i; j < n; j++)12 res.push_back(intervals[j]);13 return res;14 }15 else newInterval = merge(intervals[i], newInterval);16 }17 res.push_back(newInterval);18 return res;19 }20 private:21 Interval merge(Interval interval1, Interval interval2) {22 int start = min(interval1.start, interval2.start);23 int end = max(interval1.end, interval2.end);24 return Interval(start, end);25 }26 };

Another idea is to search for the two ends of the overlapping intervals using binary search. Then we only need to merge newInterval with the intervals at the two ends if they overlap. All the intervals within the two ends will be contained in newInterval.

Let's do an example in the problem statement: intervals = [1, 2], [3, 5], [6, 7], [8, 10], [12, 16] and newInterval = [4, 9]. We first find the rightmost interval with start smaller than that of newInterval, which is [3, 5]. Then we find the leftmost interval with end larger than that of newInterval, which is [8, 10]. Then all the intervals between them will be contained within newInterval (you may check this to convince yourself) and so can be safely ignored. We only need to check whether newInterval overlaps with the two intervals on the two ends and merge them if necessary.

The complete code is as follows.

1 class Solution { 2 public: 3     vector
insert(vector
& intervals, Interval newInterval) { 4 int n = intervals.size(), leftEnd, rightEnd, l, r; 5 vector
res; 6 // Find the rightmost interval with start smaller than that of newInterval 7 for (l = 0, r = n - 1; l <= r; ) { 8 int mid = l + ((r - l) >> 1); 9 if (intervals[mid].start > newInterval.start)10 r = mid - 1;11 else l = mid + 1;12 }13 leftEnd = r;14 // Find the leftmost interval with end larger than that of newInterval15 for (l = 0, r = n - 1; l <= r; ) {16 int mid = l + ((r - l) >> 1);17 if (intervals[mid].end < newInterval.end)18 l = mid + 1;19 else r = mid - 1;20 }21 rightEnd = l;22 // Merge newInterval with intervals[leftEnd] and intervals[rightEnd] if necessary23 if (leftEnd >= 0 && intervals[leftEnd].end >= newInterval.start)24 newInterval.start = intervals[leftEnd--].start;25 if (rightEnd < n && intervals[rightEnd].start <= newInterval.end)26 newInterval.end = intervals[rightEnd++].end;27 // Save the intervals sequentially28 for (int i = 0; i <= leftEnd; i++)29 res.push_back(intervals[i]);30 res.push_back(newInterval);31 for (int i = rightEnd; i < n; i++)32 res.push_back(intervals[i]);33 return res;34 }35 };

 

转载地址:http://dpjaa.baihongyu.com/

你可能感兴趣的文章
git回退到某个历史版本
查看>>
HTML5基础(二)
查看>>
ue4(c++) 按钮中的文字居中的问题
查看>>
Hadoop日记Day1---Hadoop介绍
查看>>
Android学习笔记——文件路径(/mnt/sdcard/...)、Uri(content://media/external/...)学习
查看>>
Echart:前端很好的数据图表展现工具+demo
查看>>
Linux VNC黑屏(转)
查看>>
Java反射简介
查看>>
day8--socket网络编程进阶
查看>>
node mysql模块写入中文字符时的乱码问题
查看>>
分析Ajax爬取今日头条街拍美图
查看>>
内存分布简视图
查看>>
如何学习虚拟现实技术vr? vr初级入门教程开始
查看>>
第4 章序列的应用
查看>>
初识闭包
查看>>
hdu1874畅通工程续
查看>>
rails 字符串 转化为 html
查看>>
AOP动态代理
查看>>
Yii2.0 下的 load() 方法的使用
查看>>
华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery 及 root
查看>>