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 ...
随机推荐
- MySQL软件基本管理
1. 忘记密码 windows平台下,5.6.43版本mysql # 关闭mysql # 在cmd中执行:mysqld --skip-grant-tables # 在cmd中执行:mysql # 执行 ...
- eMMC基础技术2:eMMC概述
0.前言 本文主要参考eMMC规范,从总体上对eMMC 进行简要介绍.主要包含如下的内容: (1)eMMC系统的总体架构 (2)eMMC的总线协议 (3)device controller (4)fl ...
- 高并发的socket的高性能设计【转】
转自:https://blog.csdn.net/quincyfang/article/details/44654351 高性能数据传输系统的框架设计 1 引言 随着互联网和物联网的高速发展,使用网络 ...
- C#代码处理前台html标签拼接
之前一篇文章是写,JavaScript处理特殊字符拼接时截断问题.最近在处理公司老软件兼容性升级时碰到的一个类似的问题,这次是后台拼接字符串,前台.aspx页面显示的.中间走了两次弯路,在此记录一下. ...
- Spring HibernateTemplate与HibernateDaoSupport对比
HibernateTemplate与HibernateDaoSupport两者都是spring整合hibernate提供的模板技术. 对于保存一个对象,HibernateTemplate需要先配置 配 ...
- C#红绿状态灯
1.在Label里 画圆,存在窗体刷新会丢失画. public void SetShowConnectStatus(Label lbl, bool isOk) { lbl.Text = "& ...
- 1-HTML Attributes
下表列举了常用的Html属性 Attribute Description alt Specifies an alternative text for an image, when the image ...
- HDU 4549
水题: 费马小定理+快速幂+矩阵快速幂 (第一次用到费马小定理) #include<bits/stdc++.h> using namespace std; typedef long lon ...
- JavaScript 加解密库(crypto-js)
1. 概述 1.1 说明 crypto-js(GitHub)是谷歌开发的一个纯JavaScript的加密算法类库,可以非常方便的在前端进行其所支持的加解密操作.目前crypto-js已支持的算法有:M ...
- hdu2602 Bone Collector 01背包
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...