当前位置:网站首页>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;
}
};
边栏推荐
- leetcode: 485. Maximum number of consecutive 1s
- OpenShift 4 - Customize RHACS security policies to prevent production clusters from using high-risk registry
- Web自动化实战——Selenium4(自动化测试环境的搭建)
- 为什么要分库分表?
- 分成两栏后文字顺序混乱的问题解决【写期刊论文时】
- element-plus虚拟表格virtual-list组件中是怎么实现清理lodash.memoize缓存的?
- /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用
- MySql总结
- R语言的画图代码及差异性分析[通俗易懂]
- LeetCode二叉树系列——110.平衡二叉树
猜你喜欢

Analysis of the startup source code of hyperf (2) - how the request reaches the controller

2021 OWASP TOP 10 漏洞指南

自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统

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

如何进行需求分析评审

Node version switching management using NVM

Getting started with UnityShader (3) - Unity's Shader

MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!

Uniapp WeChat small application reference standard components

Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
随机推荐
Spark学习(2)-Spark环境搭建-Local
Introduction to BigDecimal, common methods
ML, DL, CV common problems sorting
LeetCode二叉树系列——110.平衡二叉树
jOOQ 3.14 released - SQL/XML and SQL/JSON support
Unity Shader入门精要学习——透明效果
The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
MySQL [subquery]
看交互设计如何集成到Scrum敏捷流程中
TCP详解
Node version switching management using NVM
OAuth2:搭建授权服务器
How to clean up the lodash.memoize cache in the element-plus virtual table virtual-list component?
PDF 拆分/合并
以后面试官问你 为啥不建议使用Select *,请你大声回答他!
[QNX Hypervisor 2.2用户手册]9.14 safety
易驱线主控芯片对比(电动三轮电机90O瓦世纪通达)
OpenShift 4 - Customize RHACS security policies to prevent production clusters from using high-risk registry
OAuth2:使用JWT令牌
abaqus find contact pairs报错:surface name is already in use