435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note:
- You may assume the interval's end point is always bigger than its start point.
- Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
Example 1:
Input: [ [1,2], [2,3], [3,4], [1,3] ] Output: 1 Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
Example 2:
Input: [ [1,2], [1,2], [1,2] ] Output: 2 Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
Example 3:
Input: [ [1,2], [2,3] ] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping. 求移除多少区间后,剩余区间都是不重叠的。 先求最多能组成多少不重叠的区间,再用总区间数减去不重叠区间数。 C++:
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool compare(const Interval& a ,const Interval& b){
return a.end < b.end ;
}
class Solution {
public:
int eraseOverlapIntervals(vector<Interval>& intervals) {
if (intervals.size() == ){
return ;
}
sort(intervals.begin() , intervals.end() , compare) ;
int cnt = ;
int end = intervals[].end ;
for(int i = ; i < intervals.size() ; i++){
if (intervals[i].start < end){
continue ;
}
end = intervals[i].end ;
cnt++ ;
}
return intervals.size() - cnt ;
}
};
435. Non-overlapping Intervals的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- leetcode56. Merge Intervals
题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...
- Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【Leetcode】【Hard】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- Java for LeetCode 056 Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode]题解(python):056-Merge Intervals
题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...
随机推荐
- iris数据集(鸢尾花)
包含三个花的品种(Iris setosa(山鸢尾),Iris virginica(北美鸢尾),Iris versicolor(变色鸢尾)) 每个品种各50个样 每个样本四个特征参数(萼片长度和宽度.花 ...
- python中“*”、"*args"、"kwargs"三种用法
参考链接:https://www.cnblogs.com/cwind/p/8996000.html 注意的是: (1)"*"符号的用法很类似C++中的指针,针对列表; (2)&qu ...
- Tomcat安装7.0.91
版本升级,JDK 1.7,Tomcat从7.0.73升级到7.0.91 为什么升级?解决安全漏洞! 升级就正常流程,下载*.tar.gz ,解压,改配置. 但碰到神奇的坑: 1.server.xml中 ...
- 【windows下进程searchfilterhost.exe分析】
searchfilterhost.exe [进程信息] 进程文件: searchfilterhost.exe 进程名称: n/a 英文描述: searchfilterhost.exe is a pro ...
- Linux 网络侦错:无法联机原因分析
所谓的软件问题,绝大部分就是 IP 参数设定错误啊,路由不对啊,还有 DNS 的 IP 设定错误等等的, 这些问题都是属于软件设定啦!只要将设定改一改,利用一些侦测软件查一查,就知道问题出在哪里了!基 ...
- hibernate映射(学生-科目-成绩)
实体类 1 public class Student { 2 private int id; 3 private String name; 4 private Set<Score> sco ...
- JS 手机端多张图片上传
代码如下 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="u ...
- nginx的location、rewrite玩法详解
1. location正则写法 一个示例: 1234567891011121314151617181920212223242526272829303132333435363738394041424 ...
- 【进阶1-2期】JavaScript深入之执行上下文栈和变量对象(转)
这是我在公众号(高级前端进阶)看到的文章,现在做笔记 https://mp.weixin.qq.com/s/hZIpnkKqdQgQnK1BcrH6Nw 阅读笔记 JS是单线程的语言,执行顺序肯定是顺 ...
- swift 学习- 12 -- 方法
// 方法 是与某些特定类型相关的函数. 类, 结构体,枚举 都可以定义实例方法, 实例方法为给类型的实例封装了具体的任务与功能. 类, 结构体, 枚举 也可以定义类型方法, 类型方法与类型本身 ...