← Interval & Math

Micro-Drill #113 — Merge intervals

Interval & Math Target: 15s

Sort-then-merge is the standard O(n log n) approach to interval union.

a.sort()
res = [a[0]]
for s, e in a[1:]:
    if s <= res[-1][1]:
        res[-1][1] = max(res[-1][1], e)
    else:
        res.append([s, e])

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #112 Micro #114 →