当前位置:网站首页>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 9
Sample output:
3
Sort 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;}
边栏推荐
猜你喜欢
随机推荐
设置表头颜色
集群监控——Zabbix使用
To eliminate asynchronous callbacks, it has to be async-await
Clearance sword refers to Offer——The sword refers to Offer II 010. and the sub-array of k
树莓派温度监视关机保护脚本
pyhon爬虫之爬取图片(亲测可用)
语音识别学习资源
Catering Supply Chain Management System
太一集团全资收购火币旗下社交产品火信
字节二面被问到mysql事务与锁问题,我蚌埠住了
Understand Chisel language. 32. Chisel advanced hardware generator (1) - parameterization in Chisel
(1), the sequential storage structure of linear table chain storage structure
【LeetCode每日一题】——540.有序数组中的单一元素
JWT主动校验Token是否过期
框架整合(二)- 使用Apache ShardingSphere实现数据分片
JS中null与undefined的异同点
CAS:474922-26-4,DSPE-PEG-NH2,DSPE-PEG-amine,磷脂-聚乙二醇-氨基供应
LeetCode 899. 有序队列
消灭异步回调,还得是async-await
R语言glm函数使用频数数据构建二分类logistic回归模型,分析的输入数据为频数数据(多个分类指标对应的阴性样本和阳性样本的频数数据)、weights参数指定频数值