Program to find total unique duration from a list of intervals in Python

Suppose we have a list of intervals where each interval represents a range [start, end] (inclusive). We need to find the total unique duration covered by all intervals combined, handling any overlaps correctly.

So, if the input is like intervals = [[2, 11],[13, 31],[41, 61]], then the output will be 50, as the total unique covered distance is (11 - 2 + 1) = 10 then (31 - 13 + 1) = 19 and (61 - 41 + 1) = 21, so total is 50.

Algorithm

To solve this, we will follow these steps −

  • If intervals list is empty, return 0
  • Sort the intervals by start time
  • Initialize with the first interval [start, end]
  • For each subsequent interval, check if it overlaps with current interval
  • If no overlap, add current interval duration to answer and start new interval
  • If overlap, merge by extending the end time
  • Add the final interval duration to answer

Example

Let us see the following implementation to get better understanding −

class Solution:
    def solve(self, intervals):
        if not intervals:
            return 0
        
        intervals.sort()
        start, end = intervals[0]
        ans = 0
        
        for s, e in intervals:
            if s > end:
                # No overlap, add current interval duration
                ans += end - start + 1
                start = s
                end = e
            else:
                # Overlap, merge intervals
                end = max(end, e)
        
        # Add the last interval duration
        ans += end - start + 1
        return ans

# Test the solution
ob = Solution()
intervals = [[2, 11],[13, 31],[41, 61]]
print(ob.solve(intervals))
50

How It Works

The algorithm works by merging overlapping intervals:

  1. Sort intervals: [2,11], [13,31], [41,61]
  2. First interval [2,11]: No previous interval to compare
  3. Second interval [13,31]: 13 > 11, so no overlap. Add (11-2+1)=10 to answer
  4. Third interval [41,61]: 41 > 31, so no overlap. Add (31-13+1)=19 to answer
  5. Final interval: Add (61-41+1)=21 to answer
  6. Total: 10 + 19 + 21 = 50

Example with Overlapping Intervals

Here's an example showing how overlapping intervals are merged −

# Example with overlapping intervals
ob = Solution()
overlapping_intervals = [[1, 5], [3, 8], [10, 15]]
print(f"Overlapping intervals: {overlapping_intervals}")
print(f"Total unique duration: {ob.solve(overlapping_intervals)}")

# Step by step explanation
print("\nStep by step:")
print("1. [1,5] and [3,8] overlap ? merged to [1,8] ? duration = 8")
print("2. [10,15] is separate ? duration = 6") 
print("3. Total = 8 + 6 = 14")
Overlapping intervals: [[1, 5], [3, 8], [10, 15]]
Total unique duration: 14

Step by step:
1. [1,5] and [3,8] overlap ? merged to [1,8] ? duration = 8
2. [10,15] is separate ? duration = 6
3. Total = 8 + 6 = 14

Conclusion

This algorithm efficiently calculates total unique duration by sorting intervals and merging overlaps. The time complexity is O(n log n) due to sorting, and space complexity is O(1).

Updated on: 2026-03-25T12:27:11+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements