当前位置:网站首页>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
边栏推荐
- Prometheus remote_ write InfluxDB,unable to parse authentication credentials,authorization failed
- Cocos2d-x 游戏存档[通俗易懂]
- C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
- Word inversion implements "suggestions collection"
- 阿洛的烦恼
- Écrivez une liste de sauts
- 华泰证券可以做到万一佣金吗,万一开户安全嘛
- Codesonar Webinar
- Hoj 2245 planktonic triangle cell (Mathematics)
- 95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
猜你喜欢
Using enumeration to realize English to braille
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
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
Virtual machine network configuration in VMWare
Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
Goal: do not exclude yaml syntax. Try to get started quickly
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
ISO 26262 - considerations other than requirements based testing
Codesonar enhances software reliability through innovative static analysis
随机推荐
margin 等高布局
The difference between NPM uninstall and RM direct deletion
Guava multithreading, futurecallback thread calls are uneven
Demon daddy guide post - simple version
恶魔奶爸 A3阶段 近常速语流初接触
sqlHelper的增删改查
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
恶魔奶爸 B2 突破语法,完成正统口语练习
How to meet the dual needs of security and confidentiality of medical devices?
Unity3d 4.3.4f1执行项目
Static analysis of software defects codesonar 5.2 release
The little money made by the program ape is a P!
Cantata9.0 | new features
object-c编程tips-timer「建议收藏」
Unity3d 4.3.4f1 execution project
Tensorflow2. How to run under x 1 Code of X
npm uninstall和rm直接删除的区别
Mahout-Pearson correlation的实现
MySQL storage expression error
Deployment, recall and deletion solutions - stsadm and PowerShell "suggestions collection"