150強訪談簡單

合併間隔

“合併間隔”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個區間數組,其中區間[i] = [starti, endi],合併所有重疊區間,並傳回覆蓋輸入中所有區間的非重疊區間數組。

寫一個函數 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?
考慮使用特定於間隔的資料結構,例如集合或堆。
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 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。