Skip to content

Commit d966946

Browse files
committed
merge intervals solution
1 parent 59a0541 commit d966946

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

merge-intervals/Chapse57.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
3+
# O(nlogn)
4+
5+
intervals.sort(key = lambda i : i[0])
6+
output = [intervals[0]]
7+
8+
for start , end in intervals[1:]:
9+
lastEnd = output[-1][1] #last end value
10+
if start <= lastEnd:
11+
output[-1][1] = max(lastEnd, end)
12+
else:
13+
output.append([start, end])
14+
return output
15+

0 commit comments

Comments
 (0)