KatInterview

Minimum Concurrent Resources for Overlapping Intervals

Greedy & IntervalsAuto-graded · 20 test cases
MediumAuto-gradedGreedy & Intervalsintervalssweep-lineheap

Last updated

Find the maximum number of overlapping intervals at any instant using sweep line or heap techniques.

Given a collection of meeting time intervals, determine the minimum number of rooms needed to accommodate all meetings without any room hosting overlapping meetings.

Each meeting is represented as a tuple of integers (start, end), corresponding to the half-open interval [start, end).

Rules

  • A room can host at most one meeting at any given instant.
  • A meeting occupies a room strictly from start up to, but not including, end.
  • If one meeting ends at time $t$ and another starts at time $t$, they do not overlap and may share the same room.
  • Duplicate meeting intervals are allowed; each occurrence represents an independent meeting requiring a room.
  • If no meetings are provided, return 0.

Constraints

  • $0 \le \text{len(meetings)} \le 10^5$
  • $0 \le \text{start} < \text{end} \le 10^9$ for each meeting

Examples

Example 1

Input: min_rooms([[1, 5], [2, 6], [4, 8], [6, 9]])

Output: 3

Example 2

Input: min_rooms([[1, 3], [3, 5], [5, 7]])

Output: 1

Example 3

Input: min_rooms([[10, 20], [10, 20], [20, 30]])

Output: 2

Hints

Hint 1

The minimum number of rooms required equals the maximum number of meetings occurring concurrently at any point in time.

Hint 2

Consider transforming each meeting into two discrete events: a room becoming occupied (+1) and a room becoming available (-1).

Hint 3

Pay close attention to how events occurring at the exact same timestamp are sorted to respect the half-open interval rule [start, end).