Problem
Given an array of meeting time intervals consisting of start and end times[[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
Examples
Constraints
- 1 <= intervals.length <= 10^4
- 0 <= starti < endi <= 10^6
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 253 with 20 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 253 # by problem number
lcpy gen -s meeting_rooms_ii # by problem name
[[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
Input: [[0,30],[5,10],[15,20]]
Output: 2
Input: [[7,10],[2,4]]
Output: 1
class Solution:
# Time: O(n log n)
# Space: O(n)
def min_meeting_rooms(self, intervals: list[list[int]]) -> int:
if not intervals:
return 0
# Separate start and end times
starts = sorted([interval[0] for interval in intervals])
ends = sorted([interval[1] for interval in intervals])
rooms = 0
max_rooms = 0
start_ptr = end_ptr = 0
# Two pointer approach
while start_ptr < len(intervals):
if starts[start_ptr] < ends[end_ptr]:
# Meeting starts, need a room
rooms += 1
max_rooms = max(max_rooms, rooms)
start_ptr += 1
else:
# Meeting ends, free a room
rooms -= 1
end_ptr += 1
return max_rooms
| Time | Space |
|---|---|
| O(n log n) | O(n) |