Notice
Recent Posts
Recent Comments
Link
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- web
- javascript
- C++
- 동적계획법
- 프로그래밍
- Level2
- 고득점Kit
- sql
- LeetCode
- react
- Medium
- VUE
- 자바스크립트
- Level1
- 파이썬
- Doitvue.js입문
- 리액트
- CS
- dp
- OS
- 웹프로그래밍
- 배열
- 카카오
- typescript
- 리트코드
- Level3
- 코테연습
- python
- 백준
- 프로그래머스
Archives
- Today
- Total
[리트코드] 56. Merge Intervals 본문
반응형
581. Shortest Unsorted Continuous Subarray
문제
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.
제한사항
- 1 <= intervals.length <= 104
- intervals[i].length == 2
- 0 <= starti <= endi <= 104
풀이
각 interval을 merge해서 최종 시작점과 끝 점을 리턴해야하므로 시작점으로 정렬한다.
앞의 interval과 다음 시작점을 비교한다.
- merge가 가능하다면 -> merge end를 비교해서 더 큰 값으로 업데이트한다.
- 불가능하다면 옆에 새로운 interval을 만든다.
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort()
result = []
cnt = 0
for start, end in intervals:
if result and result[-1][1] >= start:# merge 될 때
result[-1][1] = max(result[-1][1], end)
else:# 옆에 붙일 때
result.append([start, end])
return result
반응형
'코테 문제 풀이' 카테고리의 다른 글
[리트코드] 287. Find the Duplicate Number (0) | 2022.04.13 |
---|---|
[리트코드] 581. Shortest Unsorted Continuous Subarray (0) | 2022.04.13 |
[리트코드] 162. Find Peak Element - python (0) | 2022.04.13 |
[리트코드] 88. Merge Sorted Array - python (0) | 2022.04.13 |
[리트코드] 75. Sort Colors - python (0) | 2022.04.13 |
Comments