当前位置:网站首页>Interval greedy (interval merge)
Interval greedy (interval merge)
2022-08-04 17:33:00 【Madness is free】
803. Interval Merging
given n
Intervals [li,ri]
, requires merging all intersecting intervals.
Note that if they intersect at the endpoints, there is also an intersection.
Output the number of intervals after the merge is completed.
Example: [1,3]
and [2,6] can be combined into one interval [1,6]
.
Input format
The first line contains the integer n
.
Next n
lines, each line contains two integers l and r
.
Output format
A total of one line, including an integer, indicates the number of intervals after the merge interval is completed.
Data Range
1≤n≤100000
,
−109≤li≤ri≤109
Sample input:
51 2twenty four5 67 87 9Sample output:
3Sort by the left endpoint from small to large, then by the right endpoint from small to large, and compare the right endpoint of the previous interval with the left end of the next interval each timePoints, if there is an intersection, they can be merged into a new interval, otherwise they cannot be merged into an interval.
#include #include #include #include using namespace std;typedef pair PLL;void merge(vector &vec) //merge interval{sort(vec.begin(),vec.end()); //By default, the first element is sorted from small to large, and then the second element is sortedvector res;int st=vec[0].first,ed=vec[0].second;;for(int i=1;i> n;vector vec;for(int i=0;i> l >> r;vec.push_back({l,r});}merge(vec);cout << vec.size() << endl;return 0;} 边栏推荐
- Catering Supply Chain Management System
- 谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~
- DMPE-PEG-Mal,二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺简述
- "Involution" Index Analysis Based on AHP
- 设置表头颜色
- 安装失败怎么办
- Clearance sword refers to Offer——The sword refers to Offer II 010. and the sub-array of k
- 并发编程原理学习-reentrantlock源码分析
- 使用Redis做某个时间段在线数统计
- RecyclerView 缓存与复用机制
猜你喜欢
随机推荐
【日记】UPNP功能会允许自动给光猫追加端口映射
The second step through MySQL in four steps: MySQL index learning
安装失败怎么办
arm交叉编译
第一章 对象和封装
小程序经典案例
Codeforces Round #811 (Div. 3)
【LeetCode Daily Question】——374. Guess the size of the number
【技术积累】JS事件循环,Promise,async/await的运行顺序
Boost库学习笔记(一)安装与配置
Json的FastJson与Jackson
小程序笔记3
Matlab画图1
DSPE-PEG-DBCO,DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔科研实验用
软件测试高频面试题真实分享/网上银行转账是怎么测的,设计一下测试用例。
【技术笔记】树莓派4B开机流程整理(无显示器安装)
餐饮供应链管理系统
dotnet remoting 抛出异常
公司自用的国产API管理神器
如何让 JS 代码不可断点








