Skip to content

Commit

Permalink
Update 0056-merge-intervals.py
Browse files Browse the repository at this point in the history
  • Loading branch information
raghadala committed Jul 6, 2024
1 parent 82572b7 commit 5f65062
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions 16_Intervals/02_Merge_Intervals/0056-merge-intervals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []

intervals.sort(key=lambda x: x[0])
result = [intervals[0]]

for interval in intervals[1:]:
intervals.sort(key=lambda x:x[0]) #sort interval my first element (start time)
result = [intervals[0]] #start with first interval
for interval in intervals:
#if start time is <= to ending time of last interval
if interval[0] <= result[-1][1]:
result[-1][1] = max(result[-1][1], interval[1])
else:
result.append(interval)

return result

0 comments on commit 5f65062

Please sign in to comment.