PIXELBANKv9.1.0
Menu

Merge Overlapping Incident Intervals for Downtime

Problem Statement

Compute total downtime from a set of alert intervals that may overlap or be reported by multiple monitors. Overlapping intervals count once.

Background

Given a list of (start, end) outage intervals, merge overlapping/adjacent ones and sum their lengths. Two intervals overlap if one starts at or before the other ends. The total downtime is the sum of merged interval lengths.

Your Task

def total_downtime(intervals):

Return the total covered duration (number).

Input Format

  • intervals (list of (start, end) numeric tuples, start <= end).

Output Format

  • A number (total downtime).

Sample

print(total_downtime([(0, 10), (5, 15), (20, 25)]))

Output:

20

Example:

Input:
print(total_downtime([(0, 10), (5, 15), (20, 25)]))
Output:
20
Reasoning:
  • Sort the input intervals by their start times to process them in chronological order: [(0,10),(5,15),(20,25)][(0, 10), (5, 15), (20, 25)].
  • Initialize the current merged interval with the first pair, setting the current start to 00 and current end to 1010.
  • Process the next interval (5,15)(5, 15): since its start (55) is less than or equal to the current end (1010), the intervals overlap; update the current end to the maximum of 1010 and 1515, resulting in a merged interval of [0,15][0, 15].
  • Process the final interval (20,25)(20, 25): since its start (2020) is greater than the current end (1515), there is no overlap; add the length of the previous merged interval (15−0=1515 - 0 = 15) to the total and start a new current interval [20,25][20, 25].
  • Add the length of the last remaining interval to the total: 15+(25−20)=15+5=2015 + (25 - 20) = 15 + 5 = 20.
  • The final output is 20

Constraints:

  • Merge intervals that overlap or touch (next.start <= cur.end).
  • Sum merged lengths.
  • Empty list -> 0.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.