We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f345476 commit 200d3e2Copy full SHA for 200d3e2
1 file changed
insert-interval/Seoya0512.py
@@ -0,0 +1,27 @@
1
+'''
2
+Time Complexity : O(N)
3
+- newInterval을 삽입할 위치 탐색 O(N)
4
+- Intervals 병합을 위해 for문으로 모든 구간을 순회 O(N)
5
+- O(N) + O(N) = O(N)
6
+
7
+Space Complexity : O(N)
8
+- output 리스트에 모든 구간을 저장 O(N)
9
10
11
+class Solution:
12
+ def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
13
+ idx = 0
14
+ while idx < len(intervals) and intervals[idx][0] < newInterval[0]:
15
+ idx +=1
16
+ intervals.insert(idx, newInterval) # newInterval을 삽입할 위치 탐색 (O(N))
17
18
+ # Intervals 병합
19
+ output = [intervals[0]]
20
+ for interval in intervals[1:]:
21
+ # 이전 구간과 겹치는 경우
22
+ if output[-1][1] >= interval[0]:
23
+ output[-1][1] = max(output[-1][1], interval[1])
24
+ else:
25
+ output.append(interval)
26
+ return output
27
0 commit comments