Meeting Rooms III

Hard · Heap & Counting · Array · Hash Table · Sorting · Heap (Priority Queue) · Simulation

Problem

There are n rooms numbered 0 to n - 1. You are given meetings, where meetings[i] = [start_i, end_i] is a meeting over the half-open interval [start_i, end_i). All start times are distinct.

Meetings are assigned as follows:

Return the room that hosted the most meetings. If several rooms tie, return the lowest-numbered one.

Examples

Input: n = 2, meetings = [[0,10],[1,2],[3,4],[5,6]]
Output: 1

Room 0 is busy until 10, so the three short meetings all go to room 1.

Input: n = 2, meetings = [[0,5],[1,3],[2,4],[6,7]]
Output: 0

[2,4) waits for room 1 and runs [3,5); [6,7) takes room 0, so both rooms host two meetings and room 0 wins the tie.

Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
Output: 1

Rooms 0, 1 and 2 take the first three. [4,9) waits for room 2 (free at 5) and runs [5,10); [6,8) waits for room 1 (free at 10, tied with room 2 but lower) and runs [10,12). Rooms 1 and 2 host two each, and room 1 is the lower.

Constraints

Approaches

Follow-up

What if meetings could be cancelled, or rooms had different capacities? Cancellations call for lazy deletion in the busy heap (skip entries that are no longer valid when they reach the top). With capacities, the free set becomes “smallest room that fits”: a sorted structure keyed by capacity instead of a plain heap of room numbers.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…