Loading
Loading
Two volunteers have each published their free windows for the week. Each list is sorted by start and its windows are disjoint. A window [start, end] is closed: both ends are free hours.
Return every period when both volunteers are free, sorted by start and flattened into one list: start₁, end₁, start₂, end₂, … A single shared hour, such as [4, 4], counts. If they are never free together, return an empty list.
first = [[0, 2], [5, 10], [13, 23]], second = [[1, 5], [8, 12], [15, 24]][0,2] ∩ [1,5] = [1,2]first window ends first → advance first[5,10] ∩ [1,5] = [5,5]second ends first → advance second[5,10] ∩ [8,12] = [8,10]advance first[13,23] ∩ [8,12] = noneadvance second[13,23] ∩ [15,24] = [15,23]advance first — done[1, 2, 5, 5, 8, 10, 15, 23]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 0 | 2 |
|---|---|---|
| 1 | 5 | 10 |
| 2 | 13 | 23 |
| 0 | 1 | 5 |
|---|---|---|
| 1 | 8 | 12 |
| 2 | 15 | 24 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n + m) time · O(n + m) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 0 | 2 |
|---|---|---|
| 1 | 5 | 10 |
| 2 | 13 | 23 |
| 0 | 1 | 5 |
|---|---|---|
| 1 | 8 | 12 |
| 2 | 15 | 24 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Shared periods: [1,2], [5,5], [8,10] and [15,23].
Example 2
| 0 | 1 | 3 |
|---|---|---|
| 1 | 5 | 9 |
| 0 | 3 | 6 |
|---|---|---|
| 1 | 8 | 12 |
They share hour 3, then hours 5 to 6, then hours 8 to 9.