Skip to content
AI360Xpert
Beta
Difficulty: MediumIntervals

Meeting Rooms II

Problem in Plain English

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.

Problem Statement

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.

Constraints

  • 1≤n=intervals.length≤1041 \leq n = \mathrm{intervals.length} \leq 10^4.
  • Every interval contains exactly two integer endpoints.
  • 0≤starti<endi≤1060 \leq \mathrm{start}_i < \mathrm{end}_i \leq 10^6.

Examples

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.

Example 2

Input
intervals = [[7,10],[2,4]]
Output
1

Sorting reveals that [2,4] finishes before [7,10] begins. One room suffices even though the input is unsorted.

Example 3

Input
intervals = [[1,5],[5,10]]
Output
1

At time 5 the first meeting releases its room before the second begins. Equal end and start times do not overlap.

Example 4

Input
intervals = [[2,8],[2,8],[2,8]]
Output
3

All three meetings are active together from time 2 until time 8; duplicate meetings still need distinct rooms.

Example 5

Input
intervals = [[0,1000000]]
Output
1

A single positive-duration meeting needs one room regardless of its length.

Intuition

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.

Approaches

Room List Scan

Solution Details

Reveal Room List Scan: intuition, complexity, and code

Hints

Hint 1
Only the count of simultaneous meetings matters, not which meeting sits in which room.
Hint 2
Splitting every interval into a start event and an end event lets the two kinds be sorted independently.
Hint 3
Sweep the sorted starts against the sorted ends: a start before the earliest end needs a new room, otherwise a room frees up first.

Edge Cases

  • Back-to-back meetings [1,5] and [5,10] reuse one room, so the answer is 1.
  • All meetings identical need one room per meeting.
  • A single meeting needs exactly one room.

Common Mistakes and Interview Tips

  • Using start <= end to open a room makes [1,5] and [5,10] require two rooms. Start must be strictly earlier; process the end first on equality.
  • Returning used after the last start rather than best can miss an earlier peak, as in [[0,2],[0,2],[3,4]], whose answer is 2 even though final used is 1.
  • Subtracting an end and also advancing the start in the same branch skips a start event. Each branch advances only its own pointer.
  • Sorting the input array in place violates the preserved-input contract of these snippets. Sort copied row references or endpoint arrays.
  • In JavaScript the default sort orders values as text. Supply a numeric comparator to avoid misordering times such as 2 and 10.

Key Takeaway

Separating starts from ends turns room allocation into event counting: the peak number of open meetings is the minimum number of rooms.