Loading
Loading
A shared workshop keeps a log of booked time slots. Each row [start, end] is a closed range of hours: the bench is busy from start through end, inclusive. The log is in no particular order and slots often overlap.
Merge the log into the fewest non-overlapping busy blocks, where two slots belong to the same block if they overlap or touch (one ends at the hour another starts). Return the blocks sorted by start, flattened into one list: start₁, end₁, start₂, end₂, …
slots = [[1, 3], [8, 10], [2, 6], [15, 18]][1,3] [2,6] [8,10] [15,18]sorted by startblock [1,3]open the first blockblock [1,6]2 ≤ 3, so [2,6] extends it[1,6] | [8,10]8 > 6 — close the block, open a new one[1,6] | [8,10] | [15,18]flatten: 1 6 8 10 15 18[1, 6, 8, 10, 15, 18]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 3 |
|---|---|---|
| 1 | 8 | 10 |
| 2 | 2 | 6 |
| 3 | 15 | 18 |
[1,3] and [2,6] merge into [1,6]; the others stand alone.
Example 2
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 log n) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 3 |
|---|---|---|
| 1 | 8 | 10 |
| 2 | 2 | 6 |
| 3 | 15 | 18 |
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.
| 0 | 4 | 5 |
|---|---|---|
| 1 | 1 | 4 |
The slots touch at hour 4, so they merge into [1,5].