Example 1
- Input
- intervals = [[0,30],[5,10],[15,20]]
- Output
- 2
The long meeting overlaps each shorter one. The shorter meetings do not overlap each other, so they share a second room.
You are given a list of meeting time intervals and must find the smallest number of rooms so that overlapping meetings never share one. Return that minimum room count.
Given a nonempty list intervals of meetings [start, end], return the minimum number of rooms needed to host them without conflicts. Each meeting has positive duration. A room is free at its meeting end, so a meeting starting at the same instant may reuse it; occupancy is the half-open interval from start inclusive to end exclusive. Input order is arbitrary.
The implementations below preserve the input order and row values. Only the room count is returned, not a schedule.
The long meeting overlaps each shorter one. The shorter meetings do not overlap each other, so they share a second room.
Sorting reveals that [2,4] finishes before [7,10] begins. One room suffices even though the input is unsorted.
At time 5 the first meeting releases its room before the second begins. Equal end and start times do not overlap.
All three meetings are active together from time 2 until time 8; duplicate meetings still need distinct rooms.
A single positive-duration meeting needs one room regardless of its length.
The required rooms equal the largest number of meetings active at the same instant. In Example 1, [0,30] overlaps [5,10], forcing two rooms, but its second room becomes free before [15,20] starts. Unlike closed-interval merging, a meeting occupies a room from its start up to but excluding its end. Thus a meeting ending at 5 and another starting at 5 may share one room. We can simulate actual rooms or count starts and ends without assigning room identities.
Reveal Room List Scan: intuition, complexity, and code
[1,5] and [5,10] reuse one room, so the answer is 1.[1,5] and [5,10] require two rooms. Start must be strictly earlier; process the end first on equality.[[0,2],[0,2],[3,4]], whose answer is 2 even though final used is 1.2 and 10.Separating starts from ends turns room allocation into event counting: the peak number of open meetings is the minimum number of rooms.