当前位置:网站首页>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;}
边栏推荐
- R语言glm函数使用频数数据构建二分类logistic回归模型,分析的输入数据为频数数据(多个分类指标对应的阴性样本和阳性样本的频数数据)、weights参数指定频数值
- 两个对象相同数据赋值
- R语言ggpubr包的ggline函数可视化折线图、设置add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、设置折线和数据点边框颜色
- R语言计算时间序列数据的逐次差分(successive differences):使用diff函数计算时间序列数据的逐次差分值
- arm交叉编译
- The use of QCompleter for Qt auto-completion
- 小程序经典案例
- 【LeetCode Daily Question】——374. Guess the size of the number
- 开发一套高容错分布式系统
- 荣耀发布开发者服务平台,智慧生态合作提速
猜你喜欢
随机推荐
【MySQL】数据库的4中隔离级别
JWT主动校验Token是否过期
学习探索-网站中引入百度统计
(一)、线性表的顺序存储结构链式存储结构
树莓派利用autofs自动挂载/卸载外部硬盘
PT100铂热电阻三种测温方法介绍
字节二面被问到mysql事务与锁问题,我蚌埠住了
【图像分类】2021-DeiT
php如何查询字符串以什么开头
微信jsApi调用失效的相关问题
又一款高颜值 Redis 官方可视化工具,功能真心强大!
Cron表达式
mmdetection/mmdetection3d多机多卡训练
SRM供应商协同管理系统功能介绍
从-99打造Sentinel高可用集群限流中间件
R语言ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用ggarrange函数将表格数据和可视化图像组合起来(表格数据在可视化图像下方)
Fork/Join框架
init和destory方法
C# Sqlite database construction and use skills
【web自动化测试】Playwright快速入门,5分钟上手