LintCode #30. Insert Interval (Easy)

LeetCode #57. Insert Interval (Hard)

class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> v;
auto it = intervals.begin();
while (it != intervals.end() && it->end < newInterval.start) {
v.push_back(*it);
++it;
}
auto left = it;
while (it != intervals.end() && it->start <= newInterval.end) ++it;
auto right = it;
if (right - left != 0) {
v.push_back(Interval(min(left->start, newInterval.start), max((right - 1)->end, newInterval.end)));
} else {
v.push_back(newInterval);
}
while (it != intervals.end()) {
v.push_back(*it);
++it;
}
return v;
}
};

思路:

  • 第一个while循环跳过所有不会与newInterval相交且小于newInterval的区间.
  • 中间一段计算相交区间.
  • 最后一个while循环跳过所有不会与newInterval相交且大于newInterval的区间.

时间复杂度: O(n)

空间复杂度: O(n)


参照九章算法的JAVA解法改写的C++版.

class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> v;
int pos = 0;
for (auto interval : intervals) {
if (interval.end < newInterval.start) {
v.push_back(interval);
++pos;
} else if (interval.start > newInterval.end) {
v.push_back(interval);
} else {
newInterval.start = min(newInterval.start, interval.start);
newInterval.end = max(newInterval.end, interval.end);
}
}
v.insert(v.begin() + pos, newInterval);
return v;
}
};

这算法简洁在于: 一个循环, 通过两个if滤掉所有不相交的区间; (若有)剩下的区间一定与newInterval相交, 将这些区间合并到newInterval里, 最后插入即可.

唯一欠缺的地方在于插入操作会有额外的时间开销.

[OJ] Insert Interval的更多相关文章

  1. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  2. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  4. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  5. [Swift]LeetCode57. 插入区间 | Insert Interval

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

  6. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  9. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

随机推荐

  1. java ssh框架入门

    源码:http://pan.baidu.com/s/1hspUOKG

  2. updatepannel的使用

    注意:放在updatepannel中的数据,单击事件之后,只重新加载后台数据,不加载前台数据,所以如果页面上有js的插件,对js插件的引用和赋值不要放在updatepannel中,同时尽量减小upda ...

  3. EF6调用存储过程,返回多个结果集处理

    链接:http://www.codeproject.com/Articles/675933/Returning-Multiple-Result-Sets-from-an-Entity-Fram 案例: ...

  4. 20151222jquery学习笔记--验证注册表单

    $(function () { $('#search_button').button({ icons : { primary : 'ui-icon-search', }, }); $('#reg'). ...

  5. eclipse和android studio导入工程的错误

    eclipse中导入工程,需要注意导入的工程是什么,android 工程和java工程是有区别的.如果导入错误了,调起来也比较麻烦.因为入口错了呀. 特别在android studio工程,从其它人的 ...

  6. C++ socket开发1

    服务端 setlocale(LC_ALL,"Chinese-simplified"); WORD wVersionRequested; WSADATA wsaData; int e ...

  7. HDU 3920 Clear All of Them I(DP + 状态压缩 + 贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3920 题目大意:你在一个位置用激光枪灭敌人,给你初始位置,下面是2*n个敌人的位置,你一枪能杀两个,可 ...

  8. (LightOJ 1004) Monkey Banana Problem 简单dp

    You are in the world of mathematics to solve the great "Monkey Banana Problem". It states ...

  9. (转)C++静态库与动态库

    转自:http://www.cnblogs.com/skynet/p/3372855.html C++静态库与动态库 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别, ...

  10. apache常用工作模式的区别

    apache常用工作模式有两种 worker模式1.线程模式2.占用资源少3.稳定性略差4.并发大 prefork模式1.进程模式2.占用资源多3.稳定4.并发一般  来自为知笔记(Wiz)