Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create merge_interval.py #115

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Python/merge_interval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Given an array of intervals where intervals[i] = [starti, endi],
# merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

def merge(intervals):
if intervals==[]:
return []
result=[]
intervals.sort()
for interval in intervals:
if result==[] or result[-1][1]<interval[0]:
result.append(interval)
else:
result[-1][1]=max(result[-1][1],interval[1])
return result

if __name__ == "__main__":
intervals=[[1,3],[2,5]]
print(merge(intervals))