Problem statement

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

Solution

Use Leetcode 0056. Merge Intervals, then choose the longest interval

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 max(b - a + 1 for a, b in ans)