KatInterview

Count Rooms In Use at Peak Time

Greedy & IntervalsAuto-graded · 15 test cases
EasyAuto-gradedGreedy & Intervalsgreedysweep lineintervals

Last updated

Tests the sweep-line technique by finding the maximum number of meeting rooms simultaneously occupied across a set of half-open booking intervals.

Count Rooms In Use at Peak Time

A building has an unlimited number of meeting rooms. You are given a list of bookings, where each booking is represented as [start, end] — a half-open interval [start, end) measured in minutes from midnight. A room is considered occupied for the entire duration start ≤ t < end.

Your task is to find the maximum number of rooms that are in use at the same time across all bookings.

Two bookings [s1, e1) and [s2, e2) overlap if and only if they share at least one common minute, i.e. max(s1, s2) < min(e1, e2).

Return 0 if no bookings are provided.

Constraints

  • 0 ≤ len(bookings) ≤ 2000
  • Each booking is a list of exactly two integers [start, end].
  • 0 ≤ start < end ≤ 1440 (minutes in a day)
  • Multiple bookings may have identical [start, end] values.

Examples

Example 1

Input: peak_rooms_in_use([[0, 60], [30, 90], [60, 120]])

Output: 2

Example 2

Input: peak_rooms_in_use([[0, 30], [30, 60], [60, 90]])

Output: 1

Example 3

Input: peak_rooms_in_use([])

Output: 0

Hints

Hint 1

Think of each booking as two events: a +1 at the start minute and a -1 at the end minute. Process these events in sorted order.

Hint 2

When two events fall on the same minute, make sure you process all end events (-1) before start events (+1) — this correctly handles the half-open interval semantics.

Hint 3

Keep a running counter of currently active rooms as you sweep through the sorted events, and track its maximum value.