当前位置:网站首页>Buckle practice - 30 set the intersection size to at least 2
Buckle practice - 30 set the intersection size to at least 2
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
30 Set the intersection size to at least 2
1. Problem description
An integer interval [a, b] ( a < b ) It's about starting from a To b All consecutive integers of , Include a and b.
Give you a set of integer intervals intervals, Please find a minimum set S, bring S Elements and intervals in intervals Every integer interval in has at least 2 Elements intersect .
Output this minimal set S Size .
Example 1:
Input : intervals = [[1, 3], [1, 4], [2, 5], [3, 5]]
Output : 3
explain :
Consider the set S = {2, 3, 4}. S And intervals All four intervals in have at least 2 Intersecting elements .
And this is S In the smallest case , So we export 3.
Example 2:
Input : intervals = [[1, 2], [2, 3], [2, 4], [4, 5]]
Output : 5
explain :
The smallest set S = {1, 2, 3, 4, 5}.
The following can be used: main function :
int main()
{
int m,n,data;
vector<vector<int> > intervals;
cin>>m;
for(int j=0; j<m; j++)
{
vector<int> aRow;
for(int i=0; i<2; i++)
{
cin>>data;
aRow.push_back(data);
}
intervals.push_back(aRow);
}
int res=Solution().intersectionSizeTwo(intervals);
cout<<res;
return 0;
}
2. Enter description
First type intervals The number of intervals of m( The scope is [1, 3000]),
Then input m That's ok , Each row 2 A digital ( [0, 10^8] Range of integers ), Represents the left of the interval 、 Right border .
3. The output shows that
Output an integer
4. Example
Input
4
1 2
2 3
2 4
4 5
Output
5
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<stack>
using namespace std;
bool cmp(vector<int>&a, vector<int> &b)// Define a comparison function , Pay attention to the writing of parameters here
{
return a[1]<b[1]||(a[1]==b[1]&&a[0]>b[0]);// First according to The second element Ascending comparison , Press if equal First element Descending
}
int intersectionSizeTwo(vector<vector<int> > &intervals)
{
//1. First, in ascending order according to the right endpoint , Left endpoint descending
sort(intervals.begin(), intervals.end(), cmp);
//2. Define the initial result array
vector<int>v{
-1,-1 };
//3. Traverse
for (auto val : intervals)
{
int len = v.size();
if (val[0] <= v[len - 2]) continue;// There must be two repeating elements , So there are two repetitions without adding elements
if (val[0] > v.back())
v.push_back(val[1] - 1);// explain v There is no intersection between the array and the current interval , So here we add the two largest elements
v.push_back(val[1]);
}
return v.size()-2;
}
int main()
{
int m, n, data;
vector<vector<int> > intervals;
cin >> m;
for (int j = 0; j < m; j++)
{
vector<int> aRow;
for (int i = 0; i < 2; i++)
{
cin >> data;
aRow.push_back(data);
}
intervals.push_back(aRow);
}
int res = intersectionSizeTwo(intervals);
cout << res<<endl;
return 0;
}
边栏推荐
- 如何将Typora中图片上传到csdn
- Day5: construct program logic
- 一文看懂MES系统能实现企业哪些目标
- Aruba learning notes 04 Web UI -- Introduction to configuration panel
- Differences between JS map and foreach
- 1184. Distance between bus stops: simple simulation problem
- MySQL creates partition tables and automatically partitions them by day
- Convergence rules for 4 * 4 image weights
- L1-059 敲笨钟
- Guys, do you need to configure anything to use rocksdb when using flinksql? Or do you need any jar packages
猜你喜欢

在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制

1184. Distance between bus stops: simple simulation problem

leetcode:51. N 皇后

QT notes - qtxml

安装jmeter

TypeNameExtractor could not be found

如何将Typora中图片上传到csdn

Install MariaDB columnstore (version 10.3)

QT | summary of the use of edit box

What is prescaler in STM32
随机推荐
One of his birds sold for 60million -- the collection of eight mountain people in the Ming and Qing Dynasties
Use of multithreading in QT
Oracle 11.2.0.4 ASM single instance does not start automatically with system startup
[mathematical basis of Cyberspace Security Chapter 9] finite field
如何最快找出复杂代码运行时的函数调用流程
【我也想刷穿 LeetCode啊】468. 验证IP地址
如何将Typora中图片上传到csdn
Qt5.12 + vs2019 cannot locate the program input point in the dynamic link library
安装jmeter
L1-049 seat allocation of ladder race
C Advanced - data storage
JS image to Base64
Miss waiting for a year! Baidu super chain digital publishing service is limited to 50% discount
理解数据的存与取
Common shortcuts to VIM editor
Counter attack dark horse: devdbops training, give you the best courses!
The biggest crisis for testers in the workplace is not at the age of 30, but being laid off in middle age
微信公众号开发:素材管理(临时、永久)
CCF 201803_ 1 jump jump
1184. Distance between bus stops: simple simulation problem