Loading
Loading
A recording studio's calendar is a list of booked blocks, sorted by start, with no two blocks overlapping or touching. Each block [start, end] is closed: both ends are booked.
A new booking arrives. Add it to the calendar, merging it with every block it overlaps or touches, and return the updated calendar flattened into one list: start₁, end₁, start₂, end₂, … The result must still be sorted and disjoint.
calendar = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], booking = [4, 8][1,2]ends before 4 — copy unchanged[3,5] [6,7] [8,10]each reaches into [4,8] — absorb themmerged [3,10]start = min(3,4), end = max(10,8)[12,16]starts after 10 — copy unchanged[1, 2, 3, 10, 12, 16]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 3 |
|---|---|---|
| 1 | 6 | 9 |
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) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 3 |
|---|---|---|
| 1 | 6 | 9 |
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.
[2,5] overlaps [1,3], giving [1,5]; [6,9] is untouched.
Example 2
| 0 | 1 | 2 |
|---|---|---|
| 1 | 3 | 5 |
| 2 | 6 | 7 |
| 3 | 8 | 10 |
| 4 | 12 | 16 |
[3,5], [6,7] and [8,10] all fold into [3,10].