1378. Quorum Windows

Medium3.5 points estimatedIntervalsLine SweepSorting

A volunteer network wants to find the times when enough members are free to hold a meeting. The array schedules has one entry per person, and schedules[p] lists the busy intervals [start, end) of person p inside the day [0, dayEnd). A person's own intervals come in any order and may overlap each other, so a person is simply busy at an instant if at least one of their intervals covers it.

Return every maximal span [a, b) with a < b during which at least quorum people are free at the same time, as a list of pairs sorted by a. Spans that touch (one ends exactly where the next begins) must be joined into a single span, so no two returned spans share an endpoint. Return an empty list if no such time exists.

Merge each person's intervals first, then sweep the boundaries of all people in order. The intended solution runs in O(m log m) time for m intervals in total.

Example 1

Input:
schedules = [[[1,3],[2,5]],[[0,2],[6,8]],[[4,7]]]dayEnd = 10quorum = 2
Output:
[[0,1],[2,4],[5,6],[7,10]]
Explanation:

Busy times are [1,5) for person 0, [0,2) and [6,8) for person 1, [4,7) for person 2; at least two people are free exactly on [0,1), [2,4), [5,6) and [7,10).

Example 2

Input:
schedules = [[[0,4]],[[0,4]]], dayEnd = 4, quorum = 1
Output:
[]
Explanation:

Both people are busy for the whole day, so nobody is ever free.

Example 3

Input:
schedules = [[],[[2,3]],[]], dayEnd = 5, quorum = 2
Output:
[[0,5]]
Explanation:

Persons 0 and 2 are never busy, so at least two people are free for the whole day [0,5).

Constraints

1 ≤ schedules.length (people) ≤ 50

total number of intervals over all people ≤ 500; a person may have no intervals

0 ≤ start < end ≤ dayEnd ≤ 109

1 ≤ quorum ≤ people

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(m log m)
Space
O(m)

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…