Skip to content

253. Meeting Rooms II 👍

Approach 1: Heap

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  int minMeetingRooms(vector<vector<int>>& intervals) {
    ranges::sort(intervals);

    // Store the end times of each room.
    priority_queue<int, vector<int>, greater<>> minHeap;

    for (const vector<int>& interval : intervals) {
      // There's no overlap, so we can reuse the same room.
      if (!minHeap.empty() && interval[0] >= minHeap.top())
        minHeap.pop();
      minHeap.push(interval[1]);
    }

    return minHeap.size();
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  public int minMeetingRooms(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> (a[0] - b[0]));

    // Store the end times of each room.
    Queue<Integer> minHeap = new PriorityQueue<>();

    for (int[] interval : intervals) {
      // There's no overlap, so we can reuse the same room.
      if (!minHeap.isEmpty() && interval[0] >= minHeap.peek())
        minHeap.poll();
      minHeap.offer(interval[1]);
    }

    return minHeap.size();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def minMeetingRooms(self, intervals: List[List[int]]) -> int:
    minHeap = []  # Store the end times of each room.

    for start, end in sorted(intervals):
      # There's no overlap, so we can reuse the same room.
      if minHeap and start >= minHeap[0]:
        heapq.heappop(minHeap)
      heapq.heappush(minHeap, end)

    return len(minHeap)

Approach 2: Sort

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
 public:
  int minMeetingRooms(vector<vector<int>>& intervals) {
    const int n = intervals.size();
    int ans = 0;
    vector<int> starts;
    vector<int> ends;

    for (const vector<int>& interval : intervals) {
      starts.push_back(interval[0]);
      ends.push_back(interval[1]);
    }

    ranges::sort(starts);
    ranges::sort(ends);

    for (int i = 0, j = 0; i < n; ++i)
      if (starts[i] < ends[j])
        ++ans;
      else
        ++j;

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
  public int minMeetingRooms(int[][] intervals) {
    final int n = intervals.length;
    int ans = 0;
    int[] starts = new int[n];
    int[] ends = new int[n];

    for (int i = 0; i < n; ++i) {
      starts[i] = intervals[i][0];
      ends[i] = intervals[i][1];
    }

    Arrays.sort(starts);
    Arrays.sort(ends);

    // J points to ends
    for (int i = 0, j = 0; i < n; ++i)
      if (starts[i] < ends[j])
        ++ans;
      else
        ++j;

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
  def minMeetingRooms(self, intervals: List[List[int]]) -> int:
    n = len(intervals)
    ans = 0
    starts = []
    ends = []

    for start, end in intervals:
      starts.append(start)
      ends.append(end)

    starts.sort()
    ends.sort()

    j = 0
    for i in range(n):
      if starts[i] < ends[j]:
        ans += 1
      else:
        j += 1

    return ans