Problem 379703 · easy · Level 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 = [[6, 9], [2, 5], [4, 7], [11, 14]]
Output: 10
Explanation: [2, 9] is lit (length 7) and [11, 14] is lit (length 3).
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…