-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57_InsertInterval.py
More file actions
28 lines (27 loc) · 897 Bytes
/
57_InsertInterval.py
File metadata and controls
28 lines (27 loc) · 897 Bytes
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
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def insert(self, intervals, newInterval):
"""
:type intervals: List[Interval]
:type newInterval: Interval
:rtype: List[Interval]
"""
if not intervals:
return [newInterval]
start = newInterval.start
end = newInterval.end
right = left = 0
while right < len(intervals):
if start <= intervals[right].end:
if end< intervals[right].start:
break
start = min(start, intervals[right].start)
end = max(end, intervals[right].end)
else:
left += 1
right+=1
return intervals[:left]+[Interval(start,end)]+intervals[right:]