엔지니어 게시판
LeetCode 솔루션 분류

[Interview] 56. Merge Intervals

컨텐츠 정보

본문

56. Merge Intervals
Medium
16127577Add to ListShare

Given 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.
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
전체 404 / 8 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 852 명
  • 오늘 방문자 4,464 명
  • 어제 방문자 6,975 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,536 명
알림 0