Time: 112 ms (95.63%), Space: 18.2 MB (71.72%) - LeetHub

This commit is contained in:
Deven
2022-05-02 16:46:51 -04:00
parent 54d4400363
commit 673085af54
+27
View File
@@ -0,0 +1,27 @@
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
intervals.sort(key=lambda x: x[0])
# Go throught the things and merge
newIntervals = [intervals[0]]
for i in range(len(intervals) - 1):
lastIndex = len(newIntervals) - 1
start = newIntervals[lastIndex][0]
end = newIntervals[lastIndex][1]
nextStart = intervals[i + 1][0]
nextEnd = intervals[i + 1][1]
smallestStart = min(start, nextStart)
biggestEnd = max(end, nextEnd)
if end >= nextStart:
newIntervals[lastIndex] = [smallestStart, biggestEnd]
else:
newIntervals.append(intervals[i + 1])
return newIntervals