File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/merge-intervals/description/
3
+
4
+ 문제 설명:
5
+ - 주어진 구간(interval) 리스트에서 겹치는 구간들을 병합(merge)하여 반환합니다.
6
+ - 각 구간은 [start, end] 형태입니다.
7
+ - 결과는 겹치는 구간이 없는 리스트여야 하며, 정렬된 순서로 반환합니다.
8
+
9
+ TC: O(N log N)
10
+ - intervals를 정렬하는 데 O(N log N)
11
+ - 한 번의 반복으로 병합 처리 → O(N)
12
+
13
+ SC: O(N)
14
+ - 결과 리스트(output)에 최대 N개의 구간이 저장될 수 있음
15
+ """
16
+
17
+ from typing import List
18
+
19
+ class Solution :
20
+ def merge (self , intervals : List [List [int ]]) -> List [List [int ]]:
21
+ output = []
22
+ for interval in sorted (intervals ):
23
+ # output이 비어있거나, 현재 interval이 이전 interval과 겹치지 않으면 그대로 추가
24
+ if not output or output [- 1 ][1 ] < interval [0 ]:
25
+ output .append (interval )
26
+ else :
27
+ # 겹치는 경우: 끝나는 부분을 더 긴 쪽으로 병합
28
+ output [- 1 ][1 ] = max (output [- 1 ][1 ], interval [1 ])
29
+
30
+ return output
You can’t perform that action at this time.
0 commit comments