[
intervals
sort
]
BinarySearch 0523 One Interval
Problem statement
https://binarysearch.com/problems/One-Interval/
Solution
Merge all intervals, using Leetcode 0056. Merge Intervals, then if we have one, answer is 0
, else it is ans[-1][0] - ans[0][1]
.
Complexity
It is O(n log n)
for time and O(n)
for time.
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)
if len(ans) == 1:
return 0
else:
return ans[-1][0] - ans[0][1]