Skip to content

Commit 4c9cde8

Browse files
committed
Sync LeetCode submission Runtime - 779 ms (5.77%), Memory - 51.5 MB (68.69%)
1 parent 7270897 commit 4c9cde8

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

2479-meeting-rooms-iii/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
2+
3+
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
4+
5+
<p>Meetings are allocated to rooms in the following manner:</p>
6+
7+
<ol>
8+
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
9+
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
10+
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
11+
</ol>
12+
13+
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
14+
15+
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
22+
<strong>Output:</strong> 0
23+
<strong>Explanation:</strong>
24+
- At time 0, both rooms are not being used. The first meeting starts in room 0.
25+
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
26+
- At time 2, both rooms are being used. The third meeting is delayed.
27+
- At time 3, both rooms are being used. The fourth meeting is delayed.
28+
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
29+
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
30+
Both rooms 0 and 1 held 2 meetings, so we return 0.
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
37+
<strong>Output:</strong> 1
38+
<strong>Explanation:</strong>
39+
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
40+
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
41+
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
42+
- At time 4, all three rooms are being used. The fourth meeting is delayed.
43+
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
44+
- At time 6, all three rooms are being used. The fifth meeting is delayed.
45+
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
46+
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= n &lt;= 100</code></li>
54+
<li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>meetings[i].length == 2</code></li>
56+
<li><code>0 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 5 * 10<sup>5</sup></code></li>
57+
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
58+
</ul>

2479-meeting-rooms-iii/solution.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
3+
room_availability_time = [0] * n
4+
meeting_count = [0] * n
5+
for start, end in sorted(meetings):
6+
min_room_availability_time = inf
7+
min_available_time_room = 0
8+
found_unused_room = False
9+
for i in range(n):
10+
if room_availability_time[i] <= start:
11+
found_unused_room = True
12+
meeting_count[i] += 1
13+
room_availability_time[i] = end
14+
break
15+
if min_room_availability_time > room_availability_time[i]:
16+
min_room_availability_time = room_availability_time[i]
17+
min_available_time_room = i
18+
if not found_unused_room:
19+
room_availability_time[min_available_time_room] += end - start
20+
meeting_count[min_available_time_room] += 1
21+
22+
return meeting_count.index(max(meeting_count))

0 commit comments

Comments
 (0)