Problem · Medium · 25 min
Merge the time slots
- Medium
- Arrays
- Sorting
A calendar holds time slots. Each slot is an array [start, end] of two whole numbers with start <= end. Slots can overlap.
Write mergeRanges(ranges). It returns a new array of slots, where every group of overlapping slots became one slot.
- Two slots overlap when they share at least one point.
[1, 3]and[3, 5]share the point 3, so they become[1, 5]. - A slot inside another slot disappears into it.
- Sort the result by start. No two slots in the result overlap.
- The input can be in any order.
Examples
| Call | Result |
|---|---|
mergeRanges([[1, 3], [2, 6], [8, 10], [15, 18]]) |
[[1, 6], [8, 10], [15, 18]] |
mergeRanges([[1, 3], [3, 5]]) |
[[1, 5]] |
mergeRanges([]) |
[] |
Tasks
2 to do
Task 1, not checked yet. Make
mergeRangespass the example tests.Task 2, not checked yet. Make
mergeRangespass the hidden tests.
Task 1 of 2
Make mergeRanges pass the example tests.