LeetCode 솔루션 분류
[Interview] 56. Merge Intervals
본문
56. Merge Intervals
Medium
16127577Add to ListShareGiven an array of intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Constraints:
1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
Accepted
1,637,144
Submissions
3,580,930
관련자료
-
링크
댓글 1
학부유학생님의 댓글
- 익명
- 작성일
Runtime: 396 ms, faster than 5.07% of Python3 online submissions for Merge Intervals.
Memory Usage: 18 MB, less than 84.93% of Python3 online submissions for Merge Intervals.
Memory Usage: 18 MB, less than 84.93% of Python3 online submissions for Merge Intervals.
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals = sorted(intervals, key=lambda x:(x[0], x[1]))
i = 0
while i < len(intervals) - 1:
if intervals[i][1] >= intervals[i+1][0]:
intervals[i][1] = max(intervals[i][1], intervals[i+1][1])
del intervals[i+1]
else:
i += 1
return intervals