Skip to content

Commit 880f228

Browse files
committed
feat: Solve insert-interval problem
1 parent 3fa02e1 commit 880f228

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

insert-interval/hu6r1s.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
3+
intervals.append(newInterval)
4+
intervals.sort()
5+
output = [intervals[0]]
6+
for x, y in intervals[1:]:
7+
if output[-1][1] >= x:
8+
output[-1][1] = max(y, output[-1][1])
9+
else:
10+
output.append([x, y])
11+
return output
12+
13+
14+
"""
15+
newInterval을 집어넣은 다음 정렬을 해주면 인덱스 0에 있는 값 기준으로 정렬됨
16+
대소 비교를 해서 이전 배열의 인덱스 1이 현재 배열의 인덱스 0보다 크거나 같으면
17+
값을 변경해주고 아니라면 값을 그냥 추가한다.
18+
"""

0 commit comments

Comments
 (0)