Top 150 Interview簡単

Merge Intervals

Detailed guide and Python implementation for the 'Merge Intervals' problem.

問題提起

簡単

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.

Write a function merge(intervals: List[List[int]]) -> List[List[int]].

制約
  • 1 <= len(intervals) <= 10^4
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 10^4

Example 1
Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]
Explanation

Intervals [1,3] and [2,6] overlap, merge to [1,6].

Example 2
Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]
Explanation

[1,4] and [4,5] overlap.

Need a Hint?
Consider using Intervals-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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 リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。