Leetcode#253. Meeting Rooms II
1 min readMay 2, 2022
Given an array of meeting time intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Constraints:
1 <= intervals.length <= 104
0 <= starti < endi <= 106
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
# If there are no meetings, we don't need any rooms.
if not intervals:
return 0num_rooms = 0# Separate out the start and the end timings and sort them individually.
start_timings = sorted(i[0] for i in intervals)
end_timings = sorted(i[1] for i in intervals)
L = len(intervals)# The two pointers in the algorithm: e_ptr and s_ptr.
end_pointer = 0
start_pointer = 0# Until all the meetings have been processed
while start_pointer < L:
# If there is a meeting that has ended by the time the meeting at `start_pointer` starts
print(start_timings[start_pointer],end_timings[end_pointer])
if start_timings[start_pointer] < end_timings[end_pointer]:
# Free up a room and increment the end_pointer.
num_rooms += 1
else:
end_pointer += 1start_pointer += 1return num_rooms