https://leetcode.com/problems/merge-intervals/description/
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
정렬하고 스캔하면서 작업.
지금까지 추가한 인터벌의 end값 중에 제일 큰 end값을 유지하면서 갱신해나간다.
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort()
result = [intervals[0]]
end = intervals[0][1]
for i in range(1, len(intervals)):
max_end = max(end, intervals[i][1])
if intervals[i][0] <= end:
result[-1][1] = max_end
else:
result.append(intervals[i])
end = max_end
return result
'코딩 테스트 및 알고리즘 > Grind 75 (Blind 75 Leetcode Questions)' 카테고리의 다른 글
Array: Combination Sum (0) | 2024.02.17 |
---|---|
Array : Product of Array Except Self (0) | 2024.02.17 |
Array : Non-overlapping Intervals (0) | 2024.02.15 |
LinkedList : Reorder List (0) | 2024.01.23 |
LinkedList : Sort List (0) | 2024.01.21 |