当前位置:网站首页>Niuke.com: host scheduling
Niuke.com: host scheduling
2022-06-25 11:24:00 【lsgoose】
There are two ways to do , Greed and sequencing :
Catalog
1. greedy
It is easy to think of greedy algorithm here , But how to deal with it ? It's easy to think of sorting , Here we sort the start time and end time . Then we maintain one thing :
1. The start time of the current activity
2. The last fastest ending time or the last fastest ending time
The second one here is a little difficult to understand , Let's go through the processing ideas :
Set the start time start[i] And end time end[j], The two sequences here are the results of our sorting of start time and end time
1. If start[i]>=end[j], Explain that the last activity just ended , Explain that we can draw a host directly from the previous activity , Then we don't need another host at all , At this time, we will end the previous activity and look at the next one , namely j++
2. otherwise , Explain the need to increase the number of facilitators
The code is as follows :
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
* Calculate how many hosts are needed to successfully host the event
* @param n int integer Yes n An activity
* @param startEnd int integer vector<vector<>> startEnd[i][0] Used to indicate the i The start time of the event ,startEnd[i][1] It means the first one i End time of activity
* @return int integer
*/
int minmumNumberOfHost(int n, vector<vector<int> >& startEnd) {
vector<int> start;
vector<int> end;
for(int i=0;i<n;++i){
start.push_back(startEnd[i][0]);
end.push_back(startEnd[i][1]);
}
sort(start.begin(), start.end());
sort(end.begin(), end.end());
int res=0;
int j=0;
for(int i=0;i<n;++i){
if(start[i]>=end[j]){
j++;
}else{
res++;
}
}
return res;
}
};
The time complexity here is O(nlogn)
2. Sort
Sort by heap , In essence, it still uses the previous thought , That is, the current activity borrows from the previous unfinished activity , If you can borrow , Then the number will remain the same , If you can't borrow , Then the number should be increased by one .
The specific operations are as follows :
First sort the array , according to start Time first end Sort from small to large
Then we maintain a minimum heap , This heap maintenance end time , The meaning of heap top is the earliest end time of an activity :
1. If the current active time start time is greater than or equal to the heap top , Then explain that you can borrow a host from the front , Direct stack top pop And then again push The end time of the current activity
2. otherwise , direct push
such , The number of nodes left is the minimum number of hosts
Negative numbers may occur in the implementation , So start with pushINT_MIN, To ensure that the first host is added
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
* Calculate how many hosts are needed to successfully host the event
* @param n int integer Yes n An activity
* @param startEnd int integer vector<vector<>> startEnd[i][0] Used to indicate the i The start time of the event ,startEnd[i][1] It means the first one i End time of activity
* @return int integer
*/
bool cmp(vector<int> &a, vector<int> &b){
if(a[0]==a[0]){
return a[1]<b[1];
}
return a[0]<b[0];
}
int minmumNumberOfHost(int n, vector<vector<int> >& startEnd) {
sort(startEnd.begin(), startEnd.end());
priority_queue<int, vector<int>, greater<int>> q;
q.push(INT_MIN);
for(int i=0;i<n;++i){
if(q.top()<=startEnd[i][0]){
q.pop();
}
q.push(startEnd[i][1]);
}
return q.size();
}
};
边栏推荐
- SQL注入漏洞(类型篇)
- Leetcode 1249. Remove invalid brackets (awesome, finally made)
- Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
- Jincang KFS data centralized scenario (many to one) deployment
- 16 enterprise architecture strategies
- Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)
- 子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
- Remove the problem of orange border on the desktop control in WebView
- GaussDB 集群维护案例集-sql执行慢
- Comparator(用于Arrays.sort)
猜你喜欢
随机推荐
Design and implementation of university laboratory goods management information system based on SSH
基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis
PHP如何提取字符串中的图片地址
Remove the problem of orange border on the desktop control in WebView
Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)
CMU提出NLP新范式—重构预训练,高考英语交出134高分
Jincang database kingbasees plug-in force_ view
某APP中模拟器检测分析
COSCon'22 讲师征集令
zabbix分布式系统监控
一个数学难题,难倒两位数学家
Jincang KFS data cascade scenario deployment
Comparator(用于Arrays.sort)
Use of three-level linkage plug-ins selected by provinces and cities
Causes and solutions of over fitting
Introduction to JVM principle
GaussDB others内存比较高的场景
龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
MySQL synchronous data configuration and shell script implementation