Problem statement

https://binarysearch.com/problems/Interval-Duration/

Solution

Variation of Leetcode 0056. Merge Intervals: merge and then calculate points.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, intervals):
        ans = []
        for beg, end in sorted(intervals):
            if not ans or ans[-1][1] < beg:
                ans += [[beg, end]]
            else:
                ans[-1][1] = max(ans[-1][1], end)
        return sum(y - x + 1 for x, y in ans)