Created
October 2, 2019 07:37
-
-
Save alphaCoder/bc2df9940390c5c4d770dc87f8ff97d4 to your computer and use it in GitHub Desktop.
Merge intervals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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