Problem 379703 · easy · Phase 03 Linear Management & Searching

Total Lit Street Length

intervals · sorting · merging

Each street lamp lights a closed segment [a, b] of a straight street. Given the list segments (unsorted, possibly overlapping or nested), return the total length of street that is lit by at least one lamp.

Examples

Input:  segments = [[1, 4], [2, 6], [8, 10]]
Output: 7
Explanation: [1, 6] is lit (length 5) and [8, 10] is lit (length 2).
Input:  segments = [[1, 3], [3, 5]]
Output: 4

Constraints

  • 0 <= len(segments) <= 5 * 10**4, 0 <= a <= b <= 10**9.
  • A segment with a == b contributes no length.
  • Target complexity: O(n log n).

Goals

  • Merge overlapping segments after sorting by start
  • Accumulate lengths without counting shared stretches twice
Starting Python…