-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
42 lines (33 loc) · 1.32 KB
/
solution.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
34
35
36
37
38
39
40
41
42
from typing import List
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
res = []
for interval in intervals:
if newInterval[1] < interval[0]:
res.append(newInterval)
newInterval = interval
elif newInterval[0] > interval[1]:
res.append(interval)
else:
newInterval = [min(newInterval[0], interval[0]),
max(newInterval[1], interval[1])]
res.append(newInterval)
return res
# class Solution:
# def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
# res = []
# for i in range(len(intervals)):
# if newInterval[1] < intervals[i][0]:
# res.append(newInterval)
# return res + intervals[i:]
# elif newInterval[0] > intervals[i][1]:
# res.append(intervals[i])
# else:
# newInterval = [min(newInterval[0], intervals[i][0]), max(
# newInterval[1], intervals[i][1])]
# res.append(newInterval)
# return res
if __name__ == '__main__':
intervals = [[1, 3], [6, 9]]
newInterval = [2, 5]
print(Solution().insert(intervals, newInterval))