当前位置:网站首页>The maximum number of meetings you can attend [greedy + priority queue]
The maximum number of meetings you can attend [greedy + priority queue]
2022-07-07 21:26:00 【REN_ Linsen】
greedy + Priority queue
Preface
Greed is a good type of topic to examine the ability of logical analysis , Like dynamic planning / Monotonic stack / Speed is the same as double pointer . Greed has a class of problems that are often linked to the priority queue , To be precise, it is linked to the priority of the priority queue .
One 、 The maximum number of meetings you can refer to
Two 、 greedy + Priority queue
package everyday.medium;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
// The maximum number of meetings you can attend .
public class MaxEvents {
/* target: altogether n A meeting , Every meeting lasts 1 Days or more , I can attend a new meeting every day , How many meetings can I attend at most . Be able to participate most Conference , It must be before the end of the meeting As far as possible I'll take part in , You have to choose every day endDay Minimum The meeting of , Otherwise, if you miss it, you may regret . Need to take every day as the timeline , Set the meeting schedule of the day endDay Join the priority queue , same day endDay Small open , Kick out after driving , Kick out the meeting at the end of the day . */
public int maxEvents(int[][] events) {
// Put the meeting in order by the start date , It is convenient to put the... In order i Days of endDay Put in the priority queue , Otherwise, you need to traverse the array , from O(1) -> O(N).
Arrays.sort(events, Comparator.comparingInt(o -> o[0]));
PriorityQueue<Integer> minEndDay = new PriorityQueue<>();
int curDay = 1;// The first day
int ans = 0;
for (int i = 0; i < events.length || !minEndDay.isEmpty(); ) {
// Put what can be opened on and before that day , And those who have not participated in the queue .
while (i < events.length && curDay == events[i][0]) minEndDay.offer(events[i++][1]);
// Take the smallest
if (!minEndDay.isEmpty()) {
ans++;
minEndDay.poll();
}
// Kick the end of the day schedule out of the queue .
while (!minEndDay.isEmpty() && minEndDay.peek() == curDay) minEndDay.poll();
// Open the next day's meeting options .
++curDay;
}
// Choose one every day , It is possible to choose a meeting , Maybe the meeting hasn't started yet , Get the last number of meetings .
return ans;
}
}
summary
1) Greed is a good topic to examine the ability of logical analysis , More practice .
2) Greed is easily linked to the priority of the priority queue .
reference
边栏推荐
- Do you have to make money in the account to open an account? Is the fund safe?
- Default constraint and zero fill constraint of MySQL constraint
- Codesonar Webinar
- 国家正规的股票交易app有哪些?使用安不安全
- Tensorflow2. How to run under x 1 Code of X
- Magic weapon - sensitive file discovery tool
- 使用枚举实现英文转盲文
- Datatable data conversion to entity
- 反诈困境,国有大行如何破局?
- Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
猜你喜欢
Intelligent software analysis platform embold
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
The little money made by the program ape is a P!
Default constraint and zero fill constraint of MySQL constraint
Mysql子查询关键字的使用方式(exists)
How does codesonar help UAVs find software defects?
Lex & yacc of Pisa proxy SQL parsing
MySQL约束之默认约束default与零填充约束zerofill
Usage of MySQL subquery keywords (exists)
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
随机推荐
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
Static test tool
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
FTP steps for downloading files from Huawei CE switches
Lex & yacc of Pisa proxy SQL parsing
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
POJ 3140 contents division "suggestions collection"
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Jetty:配置连接器[通俗易懂]
SQL injection error report injection function graphic explanation
Usage of MySQL subquery keywords (exists)
Codesonar enhances software reliability through innovative static analysis
Mysql子查询关键字的使用方式(exists)
Codeforces 474 F. Ant colony
Insufficient permissions
The difference between NPM uninstall and RM direct deletion
Devil daddy A0 English zero foundation self-improvement Road
Feature generation
私募基金在中國合法嗎?安全嗎?