2402. Meeting Rooms III #1914
-
Topics: You are given an integer You are given a 2D integer array Meetings are allocated to rooms in the following manner:
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number. A half-closed interval Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to efficiently allocate meeting rooms to a series of meetings based on specific rules, ensuring that meetings are assigned to the lowest-numbered available room or delayed until a room becomes free. The goal is to determine which room holds the most meetings, with ties broken by the lowest room number. Approach
Let's implement this solution in PHP: 2402. Meeting Rooms III <?php
/**
* @param Integer $n
* @param Integer[][] $meetings
* @return Integer
*/
function mostBooked($n, $meetings) {
$free_rooms = new SplMinHeap();
for ($i = 0; $i < $n; $i++) {
$free_rooms->insert($i);
}
$busy_rooms = new class extends SplMinHeap {
protected function compare($a, $b) {
if ($a[0] != $b[0]) {
return $b[0] - $a[0];
}
return $b[1] - $a[1];
}
};
usort($meetings, function($a, $b) {
return $a[0] - $b[0];
});
$current_time = 0;
$count = array_fill(0, $n, 0);
foreach ($meetings as $meeting) {
$s = $meeting[0];
$e = $meeting[1];
$current_time = max($current_time, $s);
while (!$busy_rooms->isEmpty()) {
$top = $busy_rooms->top();
if ($top[0] <= $current_time) {
$busy_rooms->extract();
$free_rooms->insert($top[1]);
} else {
break;
}
}
if ($free_rooms->isEmpty()) {
$top = $busy_rooms->extract();
$current_time = $top[0];
$free_rooms->insert($top[1]);
while (!$busy_rooms->isEmpty() && $busy_rooms->top()[0] == $current_time) {
$top = $busy_rooms->extract();
$free_rooms->insert($top[1]);
}
}
$room = $free_rooms->extract();
$duration = $e - $s;
$end_time = $current_time + $duration;
$busy_rooms->insert([$end_time, $room]);
$count[$room]++;
}
$result_room = 0;
for ($room = 1; $room < $n; $room++) {
if ($count[$room] > $count[$result_room] ||
($count[$room] == $count[$result_room] && $room < $result_room)) {
$result_room = $room;
}
}
return $result_room;
}
// Test cases
// Example 1
$n = 2;
$meetings = [[0, 10], [1, 5], [2, 7], [3, 4]];
echo mostBooked($n, $meetings) . "\n"; // Output: 0
// Example 2
$n = 3;
$meetings = [[1, 20], [2, 10], [3, 5], [4, 9], [6, 8]];
echo mostBooked($n, $meetings) . "\n"; // Output: 1
?> Explanation:
This approach efficiently manages room allocation and meeting scheduling using heap structures to handle dynamic availability, ensuring optimal performance even for large input sizes. |
Beta Was this translation helpful? Give feedback.
We need to efficiently allocate meeting rooms to a series of meetings based on specific rules, ensuring that meetings are assigned to the lowest-numbered available room or delayed until a room becomes free. The goal is to determine which room holds the most meetings, with ties broken by the lowest room number.
Approach