Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-220

Total Covered Interval Length

Problem

Compute the total length covered by a set of intervals.

intervals is a list of [start, end] pairs; overlapping and touching intervals merge into one covered span. The covered length of a span is end minus start, so zero-width intervals contribute nothing.

Return the total covered length.

Hint

After sorting by start, extend the current span while the next start is at most the current end.

Test cases

case 1
in: [[[1,3],[2,5],[8,10]]]
expected: 6
case 2
in: [[]]
expected: 0
case 3
in: [[[1,2]]]
expected: 1
case 4
in: [[[1,1]]]
expected: 0
case 5
in: [[[5,7],[1,2],[2,3]]]
expected: 4
Starter codePython
def covered_length(intervals):
    # Your code here
    pass

5 test cases · run your code against them in the editor