当前位置:网站首页>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
边栏推荐
- FTP steps for downloading files from Huawei CE switches
- 智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
- Goal: do not exclude yaml syntax. Try to get started quickly
- Addition, deletion, modification and query of sqlhelper
- Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
- Is private equity legal in China? Is it safe?
- 程序猿赚的那点钱算个P啊!
- Hoj 2245 planktonic triangle cell (Mathematics)
- Lex & yacc of Pisa proxy SQL parsing
- gridView自己定义做时间排版「建议收藏」
猜你喜欢
使用枚举实现英文转盲文
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
Solve the problem of using uni app mediaerror mediaerror errorcode -5
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
MySQL约束之默认约束default与零填充约束zerofill
Ubuntu安装mysql8遇到的问题以及详细安装过程
An overview of the latest research progress of "efficient deep segmentation of labels" at Shanghai Jiaotong University, which comprehensively expounds the deep segmentation methods of unsupervised, ro
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
Tensorflow2. How to run under x 1 Code of X
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
随机推荐
Introduction to referer and referer policy
MySQL约束之默认约束default与零填充约束zerofill
DataTable数据转换为实体
2022年在启牛开中银股票的账户安全吗?
Demon daddy guide post - simple version
MySQL storage expression error
[C language] advanced pointer --- do you really understand pointer?
AADL inspector fault tree safety analysis module
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
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
object-c编程tips-timer「建议收藏」
Static analysis of software defects codesonar 5.2 release
SQL注入报错注入函数图文详解
恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
Ten thousand word summary data storage, three knowledge points
浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
数值法求解最优控制问题(〇)——定义
Le capital - investissement est - il légal en Chine? C'est sûr?