150強訪談簡單

非重疊區間

「非重疊間隔」問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個間隔數組,其中間隔[i] = [starti, endi],傳回需要刪除以使其餘間隔不重疊的最小間隔數。

寫一個函數 eraseOverlapIntervals(intervals: List[List[int]]) -> int

約束條件
  • 1 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • -5 * 10^4 <= starti < endi <= 5 * 10^4

範例

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

[1,3] can be removed and the rest are non-overlapping.

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

Remove two [1,2] to make the rest non-overlapping.

Example 3
Input
intervals = [[1,2],[2,3]]
Output
0
Explanation

Already non-overlapping.

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 資源

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