当前位置:网站首页>435. 无重叠区间
435. 无重叠区间
2022-07-31 14:46:00 【empty__barrel】
题目:
给定一个区间的集合 intervals ,其中 intervals[i] = [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠 。
将整个重叠区间看做一个整体且计数
按照题意画分析图:
- 画分析图时应罗列出所有情况
- 至少有三个点才能模拟出几乎所有的可能情况
- 注意起点情况,终点情况,中间点情况
题解:
有很多区间重叠你需要移除恰当的区间(怎么恰当呢,看起来就复杂),我们不妨想一想应该留下哪些区间,那么与这个区间重叠的就是应该丢弃的区间。
重叠区间的基准(即与哪一个区间为重复判断这个为重叠区间)即保留什么区间——当然是左边没有元素的区间右边是否有元素可以不管。当左边有元素右边有元素那么这个区间就要删掉。
找到做题关键点、突破点。
代码:
class Solution {
public:
// 按照区间右边界排序
static bool cmp (const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.size() == 0) return 0;
sort(intervals.begin(), intervals.end(), cmp);
int result = 1; // points 不为空至少需要一支箭
for (int i = 1; i < intervals.size(); i++) {
if (intervals[i][0] >= intervals[i - 1][1]) {
result++; // 需要一支箭
}
else {
// 气球i和气球i-1挨着
intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]); // 更新重叠气球最小右边界
}
}
return intervals.size() - result;
}
};
边栏推荐
- ERROR: Failed building wheel for osgeo
- SetoolKit User Guide
- Selenium自动化中无头浏览器的应用
- Detailed guide to compare two tables using natural full join in SQL
- How to clean up the lodash.memoize cache in the element-plus virtual table virtual-list component?
- 蔚来杯2022牛客暑期多校训练营4
- LeetCode二叉树系列——110.平衡二叉树
- Node version switching management using NVM
- Spark学习(2)-Spark环境搭建-Local
- 以后面试官问你 为啥不建议使用Select *,请你大声回答他!
猜你喜欢

《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件

Why do we need to sub-library and sub-table?

Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")

OAuth2:微服务权限校验Session共享

Message queue data storage MySQL table design

Node version switching management using NVM

SetoolKit User Guide

Sentinel服务熔断和降级

2021 OWASP TOP 10 漏洞指南

“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
随机推荐
TCP详解
Use of el-tooltip
The JVM a class loader
OAuth2:微服务权限校验Session共享
Unity Shader入门精要学习——透明效果
SetoolKit User Guide
Nuget打包并上传教程
jOOQ 3.14 released - SQL/XML and SQL/JSON support
易驱线主控芯片对比(电动三轮电机90O瓦世纪通达)
【Pytorch】F.softmax()方法说明
Detailed guide to compare two tables using natural full join in SQL
Groupid(artifact id)
NC | 斯坦福申小涛等开发数据可重复分析计算框架TidyMass
Sentinel服务熔断和降级
BigDecimal 简介,常用方法
MySQL 23道经典面试吊打面试官
Selenium自动化中无头浏览器的应用
ML, DL, CV common problems sorting
学习笔记12--路径-速度分解法之局部路径搜索
The recently popular domestic interface artifact Apipost experience