You are given an integer n. There are n rooms numbered from 0 to n - 1.
You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.
Meetings are allocated to rooms in the following manner:
Each meeting will take place in the unused room with the lowest number.
If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
When a room becomes unused, meetings that have an earlier original start time should be given the room.
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[a, b) is the interval between a and bincludinga and not includingb.
Input: intervals =[[1,2],[4,5],[8,10]], rooms =1, ask =[[2,3],[3,4]]Output: [true,true]Explanation:
For the ask of [2,3], we can arrange a meeting room room0.The following is the meeting list of room0:[[1,2],[2,3],[4,5],[8,10]]For the ask of [3,4], we can arrange a meeting room room0.The following is the meeting list of room0:[[1,2],[3,4],[4,5],[8,10]]
We want to efficiently answer queries about whether a meeting can be scheduled in a room, given existing meetings. By tracking the number of rooms occupied at each time, we can quickly check if a new meeting can fit without exceeding the room limit.
classSolution {
funmeetingRoom(calendar: Array<IntArray>, k: Int, query: Array<IntArray>): BooleanArray {
calendar.sortBy { it[0] }
var n = 0for (interval in calendar) n = maxOf(n, interval[1])
val rooms = IntArray(n+1)
for (interval in calendar) {
for (t in interval[0] until interval[1]) rooms[t]++ }
val result = BooleanArray(query.size)
for (i in query.indices) {
val q = query[i]
var canSchedule = truefor (t in q[0] until q[1]) {
if (rooms[t] >= k) {
canSchedule = falsebreak }
}
result[i] = canSchedule
}
return result
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defmeetingRoom(calendar: list[list[int]], k: int, query: list[list[int]]) -> list[bool]:
calendar.sort()
n = max(interval[1] for interval in calendar)
rooms = [0] * (n+1)
for interval in calendar:
for t in range(interval[0], interval[1]):
rooms[t] +=1 result = []
for q in query:
can_schedule =Truefor t in range(q[0], q[1]):
if rooms[t] >= k:
can_schedule =Falsebreak result.append(can_schedule)
return result
pubfnmeeting_room(calendar: Vec<Vec<i32>>, k: i32, query: Vec<Vec<i32>>) -> Vec<bool> {
letmut calendar = calendar;
calendar.sort_by_key(|x| x[0]);
let n = calendar.iter().map(|x| x[1]).max().unwrap_or(0);
letmut rooms =vec![0; (n+1) asusize];
for interval in&calendar {
for t in interval[0]..interval[1] {
rooms[t asusize] +=1;
}
}
letmut result = Vec::new();
for q in&query {
letmut can_schedule =true;
for t in q[0]..q[1] {
if rooms[t asusize] >= k {
can_schedule =false;
break;
}
}
result.push(can_schedule);
}
result
}