当前位置:网站首页>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
边栏推荐
- Virtual machine network configuration in VMWare
- Tensorflow2. How to run under x 1 Code of X
- 部署、收回和删除解决方式—-STSADM和PowerShell「建议收藏」
- Ubuntu安装mysql8遇到的问题以及详细安装过程
- Demon daddy A1 speech listening initial challenge
- Datatable data conversion to entity
- awk处理JSON处理
- 201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
- [200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
- Implementation of mahout Pearson correlation
猜你喜欢

Tensorflow2. How to run under x 1 Code of X

The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function

C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)

Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future

C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
MySQL storage expression error

Intelligent software analysis platform embold
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System

【C语言】指针进阶---指针你真的学懂了吗?
Klocwork code static analysis tool
随机推荐
私募基金在中国合法吗?安全吗?
Mahout-Pearson correlation的实现
Intelligent transportation is full of vitality. What will happen in the future? [easy to understand]
Unity3d 4.3.4f1 execution project
神兵利器——敏感文件发现工具
Codeforces 474 F. Ant colony
Use camunda to do workflow design and reject operations
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
Numerical method for solving optimal control problem (0) -- Definition
Implement secondary index with Gaussian redis
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
Demon daddy A3 stage near normal speed speech flow initial contact
恶魔奶爸 A1 语音听力初挑战
Insufficient permissions
[matrix multiplication] [noi 2012] [cogs963] random number generator
The difference between NPM uninstall and RM direct deletion
Goal: do not exclude yaml syntax. Try to get started quickly
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
开户还得用身份证银行卡安全吗,我是小白不懂