forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlylaminju.py
More file actions
32 lines (26 loc) ยท 785 Bytes
/
lylaminju.py
File metadata and controls
32 lines (26 loc) ยท 785 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
29
30
31
32
'''
์๊ฐ ๋ณต์ก๋: O(n)
๊ณต๊ฐ ๋ณต์ก๋: O(n)
'''
from typing import List
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
n = len(intervals)
start, end = newInterval
i = 0
result = []
# before merging
while i < n and intervals[i][1] < start:
result.append(intervals[i])
i += 1
# merge
while i < n and intervals[i][0] <= end:
start = min(start, intervals[i][0])
end = max(end, intervals[i][1])
i += 1
result.append([start, end])
# after merging
while i < n:
result.append(intervals[i])
i += 1
return result