Created
May 8, 2018 02:43
-
-
Save jesuyedavid/3e87abd52fbdbe27ef935bb07fd84676 to your computer and use it in GitHub Desktop.
Derive one giant overlap
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 mergeIntervals(arr): | |
sorted(arr) | |
index=0 | |
for i in range(len(arr)): | |
if(index!=0 and arr[index-1][0]<=arr[i][1]): | |
while(index!=0 and arr[index-1][0]<arr[i][1]): | |
arr[index-1][1]=max(arr[index-1][1], arr[i][1]) | |
arr[index-1][0]=min(arr[index-1][0], arr[i][0]) | |
index-=1 | |
else: | |
arr[index]=arr[i] | |
index+=1 | |
for i in range(index): | |
print("["+str(arr[i][0])+","+str(arr[i][1])+ "]") | |
mergeIntervals([[1,2],[3,5],[8,20],[45,60], [2,20]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment