Top 150 Interview簡単

Non Overlapping Intervals

Detailed guide and Python implementation for the 'Non Overlapping Intervals' problem.

問題提起

簡単

Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Write a function 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?
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 リソース

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