diff --git a/merge-intervals/Chapse57.py b/merge-intervals/Chapse57.py new file mode 100644 index 000000000..0cb44cced --- /dev/null +++ b/merge-intervals/Chapse57.py @@ -0,0 +1,18 @@ +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + # O(nlogn) + + intervals.sort(key = lambda i : i[0]) + output = [intervals[0]] + + for start , end in intervals[1:]: + lastEnd = output[-1][1] #last end value + if start <= lastEnd: + output[-1][1] = max(lastEnd, end) + else: + output.append([start, end]) + return output + + + +