Skip to content

Instantly share code, notes, and snippets.

@alphaCoder
Created October 2, 2019 07:37
Show Gist options
  • Save alphaCoder/d2309e4d88152d1294b22988dfadeb03 to your computer and use it in GitHub Desktop.
Save alphaCoder/d2309e4d88152d1294b22988dfadeb03 to your computer and use it in GitHub Desktop.
Merge intervals
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if len(intervals) < 2:
return intervals
res = []
for pair in sorted(intervals, key = lambda p:p[0]):
if res and res[-1][1] >= pair[0]:
res[-1][1] = max(res[-1][1], pair[1])
else:
res.extend([pair])
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment