当前位置:网站首页>力扣练习——25 无重叠区间
力扣练习——25 无重叠区间
2022-07-22 07:48:00 【qq_43403657】
25 无重叠区间
1.问题描述
给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。
注意:
可以认为区间的终点总是大于它的起点。
区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。
示例 1:
输入: [ [1,2], [2,3], [3,4], [1,3] ]
输出: 1
解释: 移除 [1,3] 后,剩下的区间没有重叠。
示例 2:
输入: [ [1,2], [1,2], [1,2] ]
输出: 2
解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
示例 3:
输入: [ [1,2], [2,3] ]
输出: 0
解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
2.输入说明
首先输入区间集合的大小n(n<20000),然后输入n行,每行2个整数,表示区间的起始和结束。
3.输出说明
输出一个整数
4.范例
输入
4
1 2
2 3
3 4
1 3
输出
1
5.代码
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(vector<int>a, vector<int>b)
{
return a.back() < b.back();
//return a[1]<b[1];//按照第二个元素排序
//return a[0]<b[0];//按照第一个元素排序
}
int eraseOverlapIntervals(vector<vector<int>>num)
{
//1.【确定首个区间】对所有区间的右端点进行排序,确定首个区间就是所有可以选择的区间中右端点最小的那个区间【若满足最小右端点区间不止一个,随便选择其中一个区间】
//2.【确定后续区间】根据排序,找出其中与首个区间不重合并且右端点最小的区间即可
//1.特殊情况
if (num.empty()) return 0;
//2.根据右端点排序
sort(num.begin(), num.end(), cmp);
//3.确定首个区间及后续区间
int len = num.size();
int right = num[0][1];//首个区间的右端点
int ans = 1;//可以保留多少个区间
for (int i = 1; i < len; i++)
{
if (num[i][0] >= right)//下一个区间的左端点没有超过上一个区间的右端点
{
ans++;//保留区间数+1
right = num[i][1];//更新右端点
}
}
return len - ans;//需要去掉几个区间
}
int main()
{
int n,t;
cin >> n;
vector<vector<int>>num;
vector<int>tmp;
for (int i = 0; i < n; i++)
{
tmp.clear();
for (int j = 0; j < 2; j++)
{
cin >> t;
tmp.push_back(t);
}
num.push_back(tmp);
}
int res = eraseOverlapIntervals(num);
cout << res;
return 0;
}
边栏推荐
- Talking about DOM objects in depth, use four versions of demoplus to break the history.state header (more)
- Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
- Boss直聘怎么写出优秀的简历?
- postgreSQL数据库部署在linux服务器上,本机查询ms级,用windows上安装的pgadmin查询超级慢20s左右,是网络的问题还是数据库配置问题?
- [case sharing] configure the routing penetration function of IS-IS
- How does win11 close the touch pad? Three solutions for closing the touch panel in win11
- [paper summary] 2D target detection article summary, continuously updated
- 线性回归(公式推导+numpy实现)
- [harmony OS] [ark UI] [demo] loading animation
- go 切片,集合简单讲解
猜你喜欢
Data Lake (18): Flink and iceberg integrate SQL API operations
数据湖(十八):Flink与Iceberg整合SQL API操作
MySQL 增删改查(進階)
Detailed explanation of bokeh parameter setting
Conference OA project
[case sharing] configure the routing penetration function of IS-IS
Buu misc advanced
【数据库】MySQL表的增删改查(进阶)
【案例分享】配置IS-IS的路由渗透功能
Nightmare of concurrent programs -- data competition
随机推荐
[must see for developers] [push kit] collection of typical problems of push service 1
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
[HMS core] [FAQ] [account kit] typical problem set 2
MySQL addition, deletion, modification and query (Advanced)
从0开始教你编写Makefile文件
Abnormal understanding and learning
Evolution of multi activity in different places
Do all Navicat versions support MySQL? Why can't I open the connection?
QWidget添加右键菜单
MySQL和MariaDB区别
【数据库】MySQL表的增删改查(基础)
[HMS core] [push kit] set of message classification problems
Methods of downloading literature from IEEE
【3D目标检测】稀疏卷积
PHP array sort
Teach you to write makefile files from 0
Vscode failed to install tools
Distributed (I) what is sacred about distributed systems, base and cap?
离线安装vscode
Branch and loop statements