1742. Room Labels

Medium3.5 points estimatedHeapIntervalsGreedySorting

A conference venue must hand out room labels to its bookings. Each entry meetings[i] = [start, end] occupies a room during the half-open interval from start up to but not including end, so a meeting that ends at minute 5 and one that starts at minute 5 may share a room.

Return an int[] labels of length n where labels[i] is the room label (a positive integer) for meeting i. Two meetings that overlap in time must get different labels, and the number of distinct labels used, R, must be the smallest possible, with the labels being exactly 1 through R. Many labellings can qualify and any valid one is accepted. Aim for O(n log n) time by sweeping meetings in order of start time.

Example 1

Input:
meetings = [[1,4],[2,5],[5,7]]
Output:
[1,2,1]
Explanation:

The first two meetings overlap and need labels 1 and 2; the third starts at minute 5, when both rooms are free, so it can reuse label 1.

Example 2

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

The long meeting keeps label 1 while the three short, non-overlapping meetings share label 2, so R = 2.

Example 3

Input:
meetings = [[0,5],[0,5],[0,5]]
Output:
[1,2,3]
Explanation:

All three meetings coincide, so three distinct labels are unavoidable.

Constraints

1 ≤ n ≤ 300 (n = meetings.length)
meetings[i] = [start, end]
0 ≤ start < end ≤ 109

How this problem is judged

Answers
Any valid answer is accepted. A checker tests yours against the problem's rules.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…