Skip to content

Commit

Permalink
Update 0253-meeting-rooms-ii.py
Browse files Browse the repository at this point in the history
  • Loading branch information
raghadala committed Jul 6, 2024
1 parent 93b74f3 commit 3be781f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions 16_Intervals/05_Meeting_Rooms_II/0253-meeting-rooms-ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
We can solve this problem using a priority queue (min-heap). First, we sort the intervals based on their start times. We then iterate through the sorted intervals and maintain a min-heap of end times of ongoing meetings. For each interval, if the start time is greater than or equal to the smallest end time in the min-heap, it means the current meeting can reuse an existing room, so we pop the smallest end time from the min-heap. If not, we need to allocate a new room. After processing all intervals, the size of the min-heap gives us the minimum number of meeting rooms required.
Time Complexity:
- The time complexity of this approach is O(n log n) due to the sorting step and the heap operations, where n is the number of intervals.
- The time complexity of this approach is O(n log n) due to the sorting step
Space Complexity:
- The space complexity is O(n), as we store the end times in the min-heap.
- The space complexity is O(n), additional space used for start, end
"""
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
Expand All @@ -20,12 +20,13 @@ def minMeetingRooms(self, intervals: List[Interval]) -> int:

res, rooms = 0,0
s, e = 0,0
while s < len(intervals):
if start[s] < end [s]:
s += 1
rooms += 1
else:
e += 1
while s < len(intervals):
if start[s] < end [s]: # new meeting starting while another going
s += 1 #move to next starting time
rooms += 1
else: #meeting has ended
e += 1 #move to next end time
rooms -= 1 #free up room
res = max(res, rooms)
return res

Expand Down

0 comments on commit 3be781f

Please sign in to comment.