상위 150개 인터뷰쉬움

삽입 간격

'삽입 간격' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

Intervals[i] = [starti, endi]가 i번째 간격의 시작과 끝을 나타내고 간격이 starti를 기준으로 오름차순으로 정렬되는 중첩되지 않는 간격 간격의 배열이 제공됩니다. 또한 다른 간격의 시작과 끝을 나타내는 newInterval = [start, end] 간격도 제공됩니다. 간격이 여전히 starti에 의해 오름차순으로 정렬되고 간격에 겹치는 간격이 없도록 간격에 newInterval을 삽입합니다(필요한 경우 겹치는 간격 병합).

삽입 후 반환 간격입니다.

insert(intervals: List[List[int]], newInterval: List[int]) -> List[List[int]] 함수를 작성하세요.

제약
  • 0 <= len(intervals) <= 10^4
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 10^5
  • intervals is sorted by starti in ascending order
  • newInterval.length == 2
  • 0 <= start <= end <= 10^5

Example 1
Input
intervals = [[1,3],[6,9]], newInterval = [2,5]
Output
[[1,5],[6,9]]
Explanation

The new interval [2,5] overlaps with [1,3], so they are merged into [1,5].

Example 2
Input
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output
[[1,2],[3,10],[12,16]]
Explanation

Because [4,8] overlaps with [3,5],[6,7],[8,10], they merge to [3,10].

Need a Hint?
세트나 힙과 같은 간격별 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.