Created
March 12, 2018 14:30
-
-
Save samidarko/7378e7fe566a5ccf86c16b7b50e4fdd3 to your computer and use it in GitHub Desktop.
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
def main(arr): | |
arr.sort(key=lambda x: x[0]) | |
if arr: | |
stack = [arr[0]] | |
else: | |
return [] | |
for start, end in arr[1:]: | |
interval = stack[-1] | |
if start > interval[1]: # it's a new interval | |
stack.append((start, end)) | |
else: | |
stack[-1] = (interval[0], max(interval[1], end)) # it's an interval update | |
return stack | |
if __name__ == "__main__": | |
print(main([(1, 3), (2, 4), (5, 7), (6, 8), (10, 15)])) | |
print(main([[6, 8], [1, 9], [2, 4], [4, 7]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment