-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
33 lines (26 loc) · 961 Bytes
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
#------------------------------------------------------------------------------
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
result = []
intervals.sort(key=lambda x: x.start)
for i in intervals:
# If list is empty or does not overlap
if not result or result[-1].end < i.start:
result.append(i)
# If there is overlap
else:
# Change the end of the interval to the higher value
result[-1].end = max(result[-1].end, i.end)
return result
#------------------------------------------------------------------------------
#Testing