[알고리즘]Leetcode Medium-56. Merge Intervals

Ina·2020년 8월 27일
0
post-thumbnail

Leetcode Medium-56. Merge Intervals

| 문제

Given a collection of intervals, merge all overlapping intervals.

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] overlaps, 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.

| 풀이

Runtime: 84 ms, faster than 85.43% of JavaScript online submissions for Merge Intervals.

풀이 요약

  1. 빈 배열 투입할 경우 그대로 리턴하도록 처리
  2. intervals 배열 오름차순 정렬하여 준비
  3. 반복문으로 <인터벌 겹칠때/겹치지 않을 때>를 나누어 배열을 result에 push
profile
프론트엔드 개발자. 기록하기, 요가, 등산

0개의 댓글