The Problem

Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of conference rooms required.

Meetings may overlap. Two meetings that share only an endpoint (one ends at time t and another starts at time t) do not conflict and can share the same room.

Interactive Visualization

Speed:
Meetings (sorted by start time)
Room Assignment Timeline
Min-Heap (end times) — size = rooms in use
Empty — meetings will be added as the algorithm runs
Code
import java.util.PriorityQueuefun minMeetingRooms(intervals: Array<IntArray>): Int {    if (intervals.isEmpty()) return 0    intervals.sortBy { it[0] }    val heap = PriorityQueue<Int>() // min-heap of end times    for (interval in intervals) {        val start = interval[0]        val end = interval[1]        if (heap.isNotEmpty() && heap.peek() <= start) {            heap.poll()  // reuse room        }        heap.add(end)    }    return heap.size}
Current Step
Press Step or Play to begin.

How It Works

Key Insight

If we process meetings in order of start time, the only question at each step is: can we reuse a room that has already freed up? A room is free when its meeting has ended. The meeting that frees up earliest is the one with the smallest end time — exactly what a min-heap gives us in O(log n).

The Algorithm

  • Sort meetings by start time.
  • Initialize an empty min-heap to track end times of active meetings.
  • For each meeting [start, end]:
    • If the heap is non-empty and the smallest end time ≤ current start → pop it (reuse that room).
    • Push the current meeting's end time onto the heap (either into the reused room or a new one).
  • The heap size at the end equals the minimum number of rooms needed.

Why It Works

Each element in the heap represents a room currently in use. When a meeting ends before the next one starts, we can recycle that room (pop + push keeps heap size the same). When there is a conflict, we must allocate a new room (push without pop increases heap size). The maximum heap size over the entire process is the answer — and since we only add rooms when strictly necessary, that maximum is minimal.

Complexity

Time O(n log n)
Space O(n)

Sorting takes O(n log n). Each of the n meetings involves at most one heap push and one heap pop, each O(log n). The heap stores at most n end times, so space is O(n).