当前位置:网站首页>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;
}
边栏推荐
- STM32——C语言基础
- QT notes - EventFilter event filter
- 1184. Distance between bus stops: simple simulation problem
- 【网络空间安全数学基础第9章】有限域
- C # entry series (29) -- preprocessing commands
- With the strong development of cloud native, how should enterprises seize business opportunities
- [rust] Why do I suggest you learn rust | a preliminary study of rust
- Common shortcuts to VIM editor
- [C and pointer Chapter 11] dynamic memory allocation
- 计算两个坐标经纬度之间的距离(5种方式)
猜你喜欢
![Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]](/img/a9/f080940ec7bf94ab83c922990efa62.png)
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]

20000 words detailed explanation, thoroughly understand es!

Basic usage of GCC

String matching KMP

Install MariaDB columnstore (version 10.3)

三、MFC消息映射机制实现原理

Counter attack dark horse: devdbops training, give you the best courses!

TypeNameExtractor could not be found

如何在IM系统中实现抢红包功能?

QT notes - realize form adaptation
随机推荐
L1-064 估值一亿的AI核心代码
An analysis of the CPU surge of an RFID tag management system in.Net
MySQL advanced (XVII) cannot connect to database server problem analysis
计算两个坐标经纬度之间的距离(5种方式)
Dynamic memory management
JVM visualvm: multi hop fault handling tool
[I also want to brush through leetcode] 468. Verify the IP address
【我也想刷穿 LeetCode啊】468. 验证IP地址
动态内存管理
Shengxin weekly issue 37
Day4: circular structure
Repeated calls, messages, idempotent schemes, full collation
Install MariaDB columnstore (version 10.3)
利用huggingface模型翻译英文
Guys, do you need to configure anything to use rocksdb when using flinksql? Or do you need any jar packages
leetcode:51. N 皇后
Please ask whether Oracle CDC does not support checkpointing. When the task is suspended and restarted during the real-time collection process, is the data changed
Quick check list of various XSS payloads
Top and bottom of stack
A* and JPS