Skip to content

Instantly share code, notes, and snippets.

@amarjitdhillon
Created February 11, 2022 07:46
Show Gist options
  • Save amarjitdhillon/1af524971310d7cd10b89b044a0db8d6 to your computer and use it in GitHub Desktop.
Save amarjitdhillon/1af524971310d7cd10b89b044a0db8d6 to your computer and use it in GitHub Desktop.
Merge Intervals
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# sort the intervals by the start value, in case they are not sorted by default
intervals.sort(key = lambda x: x[0])
# edge case. If num elements is one, then return the same list
if len(intervals) == 1:
return intervals
# add the first element to the result list
result = [intervals[0]]
# loop over the second element onwards as first is already added to result list
for start, end in intervals[1:]:
# get the prev end value from the result array
prevEnd = result[-1][1]
# if the start of this element is <= the prevEnd, then take the max from both's end and expand the interval
if start <= prevEnd:
# update the end in result
result[-1][1] = max(end,prevEnd)
else:
result.append([start,end])
# else return the result
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment