当前位置:网站首页>最多可以参加的会议数目[贪心 + 优先队列]
最多可以参加的会议数目[贪心 + 优先队列]
2022-07-07 17:32:00 【REN_林森】
前言
贪心是考察逻辑分析能力的好类型题目,犹如动态规划/单调栈/快慢双指针一样。贪心有一类题往往和优先队列挂钩,准确的说是和优先队列的优先挂钩。
一、最多可以参考的会议数目

二、贪心+优先队列
package everyday.medium;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
// 最多可以参加的会议数目。
public class MaxEvents {
/* target:一共n个会议,每个会议都持续1天以上,我每天都能参加一个新会议,我最多能参加多少会议。 要能参加 最多 的会议,就必须在会议结束前 尽可能 的参加,就必须在每天选endDay 最小 的会议来开,不然错过了就可能后悔。 需要以每天为时间线,将当天开会日程的endDay加入优先队列,当天endDay小的开,开完踢出,在当天结束的会议踢出。 */
public int maxEvents(int[][] events) {
// 给开会按开始日期排个序,方便按顺序把第i天的endDay放入优先队列中,否则需要遍历数组,从O(1) -> O(N)。
Arrays.sort(events, Comparator.comparingInt(o -> o[0]));
PriorityQueue<Integer> minEndDay = new PriorityQueue<>();
int curDay = 1;// 第一天
int ans = 0;
for (int i = 0; i < events.length || !minEndDay.isEmpty(); ) {
// 把当天及当天前能开的,且没参加过的加入队列。
while (i < events.length && curDay == events[i][0]) minEndDay.offer(events[i++][1]);
// 取一个最小的
if (!minEndDay.isEmpty()) {
ans++;
minEndDay.poll();
}
// 把当天结束的日程踢出队列。
while (!minEndDay.isEmpty() && minEndDay.peek() == curDay) minEndDay.poll();
// 开启下一天的会议选择。
++curDay;
}
// 每天选一个,有可能选到会议,有可能会议都还没开始,得到最后的开会次数。
return ans;
}
}
总结
1)贪心是考察逻辑分析能力的好题目,多练。
2)贪心很容易和优先队列的优先挂钩。
参考文献
边栏推荐
猜你喜欢

杰理之测试盒配置声道【篇】

Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica

Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry

Version 2.0 of tapdata, the open source live data platform, has been released

从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks

Command mode - unity

Install mysql8 for Linux X ultra detailed graphic tutorial

Kirin Xin'an won the bid for the new generation dispatching project of State Grid!

Numpy——2.数组的形状

Redis master-slave and sentinel master-slave switchover are built step by step
随机推荐
IP tools
杰理之关于 TWS 声道配置【篇】
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
Kirin Xin'an cloud platform is newly upgraded!
Time tools
Install mysql8 for Linux X ultra detailed graphic tutorial
POJ 1182: food chain (parallel search) [easy to understand]
How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
歌单11111
[Base64 notes] [suggestions collection]
Zhong Xuegao wants to remain innocent in the world
PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
testing and SQA_ Dynamic white box test [easy to understand]
吞吐量Throughout
L1-027 rental (Lua)
L1-019 who falls first (Lua)
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
2022年投资哪个理财产品收益高?
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势