当前位置:网站首页>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;} 边栏推荐
猜你喜欢

RecyclerView 缓存与复用机制

Thrift IDL Sample File

荣耀互联对外开放,赋能智能硬件合作伙伴,促进全场景生态产品融合

第一章 对象和封装

Kotlin挂起函数原理是什么

【LeetCode Daily Question】——374. Guess the size of the number

Learning and Exploration-Introducing Baidu Statistics to the Website

化学制品制造业数智化供应链管理系统:打造智慧供应体系,赋能企业产效提升

谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~

嵌入式开发:使用堆栈保护提高代码完整性
随机推荐
88.(cesium之家)cesium聚合图
2022年五一数学建模C题讲解
浅谈运用低代码技术如何实现物流企业的降本增效
【web自动化测试】Playwright快速入门,5分钟上手
小程序经典案例
mysqlbinlog 超过500g自动删除,保留7个,求大深给个版本
荣耀互联对外开放,赋能智能硬件合作伙伴,促进全场景生态产品融合
基于层次分析法的“内卷”指数分析
【日记】mysql数据库连接池
To eliminate asynchronous callbacks, it has to be async-await
基于大学生内卷行为的调查研究
微信jsApi调用失效的相关问题
信息系统项目管理师必背核心考点(六十)项目集管理
(1), the sequential storage structure of linear table chain storage structure
(一)、线性表的顺序存储结构链式存储结构
Matlab画图1
自定义组件,并在组件中注入自定义组件实现多种场景的下的组件切换
【日记】nodejs构建API框架以及RESTful API 和 JSON-RPC的取舍
区间贪心(区间合并)
安装失败怎么办