Leetcode#253. Meeting Rooms II

Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Input: intervals = [[7,10],[2,4]]
Output: 1
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 0
num_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 += 1
start_pointer += 1return num_rooms

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
svalak

svalak

Passionate about learning ; Will write about #systemdesign #DSA #algorithms #linuxinternals #technology; Painting/Poem writing are my hobbies; Voracious Reader