Skip to content

Commit 2aaea3c

Browse files
committed
update: 添加问题“2402.会议室III”的代码和题解 (#1287)
2402: AC.cpp (#1286) + word(en) - AC,35.71%,55.95% Signed-off-by: LetMeFly666 <[email protected]>
1 parent 6f9439e commit 2aaea3c

File tree

6 files changed

+282
-4
lines changed

6 files changed

+282
-4
lines changed

.commitmsg

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
2483: AC.cpp (#1284) + word(en)
2-
3-
AC,91.67%,55.95%
4-
cpp - AC,一次遍历,82.14%,98.81%
1+
2402: AC.cpp (#1286) + word(en) - AC,35.71%,55.95%

Codes/2402-meeting-rooms-iii.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-27 23:12:38
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-27 23:27:22
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// THIS CANNOT ACCESS
12+
class Solution {
13+
public:
14+
int mostBooked(int n, vector<vector<int>>& meetings) {
15+
sort(meetings.begin(), meetings.end());
16+
priority_queue<int, vector<int>, greater<>> canuse;
17+
for (int i = 0; i < n; i++) {
18+
canuse.push(i);
19+
}
20+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> inuse;
21+
vector<int> times(n);
22+
for (vector<int>& meeting : meetings) {
23+
// 先看有没有新释放的
24+
while (!inuse.empty() && inuse.top().first <= meeting[0]) {
25+
auto[_, thisRoom] = inuse.top();
26+
canuse.push(thisRoom);
27+
inuse.pop();
28+
}
29+
int thisRoom, endTime;
30+
// 看看有没有空的
31+
if (!canuse.empty()) {
32+
thisRoom = canuse.top();
33+
endTime = meeting[1];
34+
} else { // 等到第一个释放
35+
auto[freeTime, room] = inuse.top();
36+
endTime = freeTime + meeting[1] - meeting[0];
37+
inuse.pop();
38+
}
39+
canuse.pop();
40+
times[thisRoom]++;
41+
inuse.push({endTime, thisRoom});
42+
}
43+
return max_element(times.begin(), times.end()) - times.begin();
44+
}
45+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-27 23:12:38
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-27 23:36:34
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll; // 10^5个5*10^5可能会超int32!
12+
class Solution {
13+
public:
14+
int mostBooked(int n, vector<vector<int>>& meetings) {
15+
sort(meetings.begin(), meetings.end());
16+
priority_queue<int, vector<int>, greater<>> canuse;
17+
for (int i = 0; i < n; i++) {
18+
canuse.push(i);
19+
}
20+
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> inuse;
21+
vector<int> times(n);
22+
for (vector<int>& meeting : meetings) {
23+
// 先看有没有新释放的
24+
while (!inuse.empty() && inuse.top().first <= meeting[0]) {
25+
auto[_, thisRoom] = inuse.top();
26+
canuse.push(thisRoom);
27+
inuse.pop();
28+
}
29+
int thisRoom;
30+
ll endTime;
31+
// 看看有没有空的
32+
if (!canuse.empty()) {
33+
thisRoom = canuse.top();
34+
endTime = meeting[1];
35+
canuse.pop();
36+
} else { // 等到第一个释放
37+
auto[freeTime, room] = inuse.top();
38+
thisRoom = room;
39+
endTime = freeTime + meeting[1] - meeting[0];
40+
inuse.pop();
41+
}
42+
times[thisRoom]++;
43+
inuse.push({endTime, thisRoom});
44+
}
45+
return max_element(times.begin(), times.end()) - times.begin();
46+
}
47+
};
48+
49+
#if defined(_WIN32) || defined(__APPLE__)
50+
/*
51+
2
52+
[[0,10],[1,5],[2,7],[3,4]]
53+
54+
0
55+
*/
56+
int main() {
57+
int n;
58+
string s;
59+
while (cin >> n >> s) {
60+
Solution sol;
61+
vector<vector<int>> v = stringToVectorVector(s);
62+
cout << sol.mostBooked(n, v) << endl;
63+
}
64+
return 0;
65+
}
66+
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@
793793
|2397.被列覆盖的最多行数|中等|<a href="https://leetcode.cn/problems/maximum-rows-covered-by-columns/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/04/LeetCode%202397.%E8%A2%AB%E5%88%97%E8%A6%86%E7%9B%96%E7%9A%84%E6%9C%80%E5%A4%9A%E8%A1%8C%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135396524" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-rows-covered-by-columns/solutions/2591266/letmefly-2397bei-lie-fu-gai-de-zui-duo-x-0tqj/" target="_blank">LeetCode题解</a>|
794794
|2398.预算内的最多机器人数目|困难|<a href="https://leetcode.cn/problems/maximum-number-of-robots-within-budget/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/13/LeetCode%202398.%E9%A2%84%E7%AE%97%E5%86%85%E7%9A%84%E6%9C%80%E5%A4%9A%E6%9C%BA%E5%99%A8%E4%BA%BA%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142218259" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-number-of-robots-within-budget/solutions/2915925/letmefly-2398yu-suan-nei-de-zui-duo-ji-q-g730/" target="_blank">LeetCode题解</a>|
795795
|2399.检查相同字母间的距离|简单|<a href="https://leetcode.cn/problems/check-distances-between-same-letters/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/04/09/LeetCode%202399.%E6%A3%80%E6%9F%A5%E7%9B%B8%E5%90%8C%E5%AD%97%E6%AF%8D%E9%97%B4%E7%9A%84%E8%B7%9D%E7%A6%BB/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130038643" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-distances-between-same-letters/solutions/2216042/letmefly-2399jian-cha-xiang-tong-zi-mu-j-ef5q/" target="_blank">LeetCode题解</a>|
796+
|2402.会议室III|困难|<a href="https://leetcode.cn/problems/meeting-rooms-iii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/27/LeetCode%202402.%E4%BC%9A%E8%AE%AE%E5%AE%A4III/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156343312" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/meeting-rooms-iii/solutions/3867414/letmefly-2402hui-yi-shi-iiiyou-xian-dui-mx320/" target="_blank">LeetCode题解</a>|
796797
|2404.出现最频繁的偶数元素|简单|<a href="https://leetcode.cn/problems/most-frequent-even-element/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/04/13/LeetCode%202404.%E5%87%BA%E7%8E%B0%E6%9C%80%E9%A2%91%E7%B9%81%E7%9A%84%E5%81%B6%E6%95%B0%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130137031" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/most-frequent-even-element/solutions/2225011/letmefly-2404chu-xian-zui-pin-fan-de-ou-97qns/" target="_blank">LeetCode题解</a>|
797798
|2409.统计共同度过的日子数|简单|<a href="https://leetcode.cn/problems/count-days-spent-together/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/04/17/LeetCode%202409.%E7%BB%9F%E8%AE%A1%E5%85%B1%E5%90%8C%E5%BA%A6%E8%BF%87%E7%9A%84%E6%97%A5%E5%AD%90%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130194499" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-days-spent-together/solutions/2231082/letmefly-2409tong-ji-gong-tong-du-guo-de-le2a/" target="_blank">LeetCode题解</a>|
798799
|2410.运动员和训练师的最大匹配数|中等|<a href="https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/13/LeetCode%202410.%E8%BF%90%E5%8A%A8%E5%91%98%E5%92%8C%E8%AE%AD%E7%BB%83%E5%B8%88%E7%9A%84%E6%9C%80%E5%A4%A7%E5%8C%B9%E9%85%8D%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149312473" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/solutions/3722705/letmefly-2410yun-dong-yuan-he-xun-lian-s-lfbw/" target="_blank">LeetCode题解</a>|
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
title: 2402.会议室 III:优先队列大模拟
3+
date: 2025-12-27 23:38:12
4+
tags: [题解, LeetCode, 困难, 数组, 哈希表, 排序, 模拟, 大模拟, 堆(优先队列), 优先队列]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2402.会议室 III:优先队列大模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/meeting-rooms-iii/](https://leetcode.cn/problems/meeting-rooms-iii/)
11+
12+
<p>给你一个整数 <code>n</code> ,共有编号从 <code>0</code> 到 <code>n - 1</code> 的 <code>n</code> 个会议室。</p>
13+
14+
<p>给你一个二维整数数组 <code>meetings</code> ,其中 <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 表示一场会议将会在 <strong>半闭</strong> 时间区间 <code>[start<sub>i</sub>, end<sub>i</sub>)</code> 举办。所有 <code>start<sub>i</sub></code> 的值 <strong>互不相同</strong> 。</p>
15+
16+
<p>会议将会按以下方式分配给会议室:</p>
17+
18+
<ol>
19+
<li>每场会议都会在未占用且编号 <strong>最小</strong> 的会议室举办。</li>
20+
<li>如果没有可用的会议室,会议将会延期,直到存在空闲的会议室。延期会议的持续时间和原会议持续时间 <strong>相同</strong> 。</li>
21+
<li>当会议室处于未占用状态时,将会优先提供给原 <strong>开始</strong> 时间更早的会议。</li>
22+
</ol>
23+
24+
<p>返回举办最多次会议的房间 <strong>编号</strong> 。如果存在多个房间满足此条件,则返回编号 <strong>最小</strong> 的房间。</p>
25+
26+
<p><strong>半闭区间 </strong><code>[a, b)</code> 是 <code>a</code> 和 <code>b</code> 之间的区间,<strong>包括</strong> <code>a</code> 但<strong> 不包括</strong> <code>b</code> 。</p>
27+
28+
<p>&nbsp;</p>
29+
30+
<p><strong>示例 1:</strong></p>
31+
32+
<pre><strong>输入:</strong>n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
33+
<strong>输出:</strong>0
34+
<strong>解释:</strong>
35+
- 在时间 0 ,两个会议室都未占用,第一场会议在会议室 0 举办。
36+
- 在时间 1 ,只有会议室 1 未占用,第二场会议在会议室 1 举办。
37+
- 在时间 2 ,两个会议室都被占用,第三场会议延期举办。
38+
- 在时间 3 ,两个会议室都被占用,第四场会议延期举办。
39+
- 在时间 5 ,会议室 1 的会议结束。第三场会议在会议室 1 举办,时间周期为 [5,10) 。
40+
- 在时间 10 ,两个会议室的会议都结束。第四场会议在会议室 0 举办,时间周期为 [10,11) 。
41+
会议室 0 和会议室 1 都举办了 2 场会议,所以返回 0 。
42+
</pre>
43+
44+
<p><strong>示例 2:</strong></p>
45+
46+
<pre><strong>输入:</strong>n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
47+
<strong>输出:</strong>1
48+
<strong>解释:</strong>
49+
- 在时间 1 ,所有三个会议室都未占用,第一场会议在会议室 0 举办。
50+
- 在时间 2 ,会议室 1 和 2 未占用,第二场会议在会议室 1 举办。
51+
- 在时间 3 ,只有会议室 2 未占用,第三场会议在会议室 2 举办。
52+
- 在时间 4 ,所有三个会议室都被占用,第四场会议延期举办。
53+
- 在时间 5 ,会议室 2 的会议结束。第四场会议在会议室 2 举办,时间周期为 [5,10) 。
54+
- 在时间 6 ,所有三个会议室都被占用,第五场会议延期举办。
55+
- 在时间 10 ,会议室 1 和 2 的会议结束。第五场会议在会议室 1 举办,时间周期为 [10,12) 。
56+
会议室 1 和会议室 2 都举办了 2 场会议,所以返回 1 。
57+
</pre>
58+
59+
<p>&nbsp;</p>
60+
61+
<p><strong>提示:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= n &lt;= 100</code></li>
65+
<li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li>
66+
<li><code>meetings[i].length == 2</code></li>
67+
<li><code>0 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 5 * 10<sup>5</sup></code></li>
68+
<li><code>start<sub>i</sub></code> 的所有值 <strong>互不相同</strong></li>
69+
</ul>
70+
71+
72+
73+
## 解题方法:大模拟——两个优先队列
74+
75+
有没有发现这道题有严格的顺序**优先级**,使用优先队列再合适不过了。
76+
77+
先想想我们的策略,再思考具体怎么模拟:
78+
79+
> 遍历按**开始时间递增**的会议(因为开始时间早是安排会议的优先级依据),对于某个待安排的会议:
80+
>
81+
> 1. 查看到会议开始时间为止有没有新释放的会议室
82+
> 2. 查看此刻有没有空的会议室
83+
>
84+
> 1. 如果有,则使用**编号最小**的那个会议室
85+
> 2. 否则,等待到一个会议室释放为止并使用之(同一时间多会议室到期则优先释放**编号较小**的那个)
86+
87+
多清晰的流水线啊,只需要用代码将其表现出来就好了。
88+
89+
1. 对于会议,只需要按照开始时间从小到大排个序,因为会议的安排顺序是严格按照开始时间安排的且开始时间互不相同。
90+
2. 对于会议室的使用(空闲会议室),优先使用编号最小的,所以可以使用一个优先队列来存放空闲会议室。
91+
3. 对于在使用的会议室,优先释放结束时间最早的那个,有释放时间相同则优先释放编号较小的那个,所以也可以使用一个优先队列来维护。
92+
93+
注意,$10^5$个$5\times10^5$可能会超过`int32`
94+
95+
+ 时间复杂度$O(m\log m + n\log n + m\log n)$,其中$m=len(meetings)$。$m\log m$来自会议的排序,$n\log n$来自$n$个会议室初始状态的入队,$m\log n$来自每个会议的会议室分配;
96+
+ 空间复杂度$O(n)$。
97+
98+
### AC代码
99+
100+
#### C++
101+
102+
```cpp
103+
/*
104+
* @LastEditTime: 2025-12-27 23:36:34
105+
*/
106+
typedef long long ll; // 10^5个5*10^5可能会超int32!
107+
class Solution {
108+
public:
109+
int mostBooked(int n, vector<vector<int>>& meetings) {
110+
sort(meetings.begin(), meetings.end());
111+
priority_queue<int, vector<int>, greater<>> canuse;
112+
for (int i = 0; i < n; i++) {
113+
canuse.push(i);
114+
}
115+
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> inuse;
116+
vector<int> times(n);
117+
for (vector<int>& meeting : meetings) {
118+
// 先看有没有新释放的
119+
while (!inuse.empty() && inuse.top().first <= meeting[0]) {
120+
auto[_, thisRoom] = inuse.top();
121+
canuse.push(thisRoom);
122+
inuse.pop();
123+
}
124+
int thisRoom;
125+
ll endTime;
126+
// 看看有没有空的
127+
if (!canuse.empty()) {
128+
thisRoom = canuse.top();
129+
endTime = meeting[1];
130+
canuse.pop();
131+
} else { // 等到第一个释放
132+
auto[freeTime, room] = inuse.top();
133+
thisRoom = room;
134+
endTime = freeTime + meeting[1] - meeting[0];
135+
inuse.pop();
136+
}
137+
times[thisRoom]++;
138+
inuse.push({endTime, thisRoom});
139+
}
140+
return max_element(times.begin(), times.end()) - times.begin();
141+
}
142+
};
143+
144+
#if defined(_WIN32) || defined(__APPLE__)
145+
/*
146+
2
147+
[[0,10],[1,5],[2,7],[3,4]]
148+
149+
0
150+
*/
151+
int main() {
152+
int n;
153+
string s;
154+
while (cin >> n >> s) {
155+
Solution sol;
156+
vector<vector<int>> v = stringToVectorVector(s);
157+
cout << sol.mostBooked(n, v) << endl;
158+
}
159+
return 0;
160+
}
161+
#endif
162+
```
163+
164+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/156343312)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/27/LeetCode%202402.%E4%BC%9A%E8%AE%AE%E5%AE%A4III/)~
165+
>
166+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,9 @@ categories: [自用]
17001700
|sterling|n. 英镑<br/>adj. 优秀的,杰出的,纯银的|
17011701
|herbal|adj. 药草的,香草的<br/>n. 草本植物志|
17021702
|arbitration|n. 仲裁|
1703+
|||
1704+
|strap|n. 袋子,皮带<br/>捆扎|
1705+
|novelette|n. 中篇小说|
17031706

17041707
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
17051708
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)