Notice Period

Merge Shift Blocks

medium

Warehouse staff sign up for shifts in a free-for-all form, so the roster is full of overlapping blocks: [start, end] pairs in any order. Payroll needs the consolidated picture — the minimal set of non-overlapping blocks covering exactly the same time.

Given blocks, merge every group of overlapping blocks and return the result sorted by start. Blocks that merely touch (one ends where another begins) merge too.

Example 1
in blocks = [[1, 3], [2, 6], [8, 10], [15, 18]]
out [[1, 6], [8, 10], [15, 18]]
[1, 3] and [2, 6] overlap, fusing into [1, 6].
Example 2
in blocks = [[1, 4], [4, 5]]
out [[1, 5]]
Touching counts: they fuse.
Constraints
  • · 1 <= len(blocks) <= 2000
  • · 0 <= start <= end <= 10^6
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.