Top 150 InterviewEasy

Non Overlapping Intervals

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

Problem Statement

Easy

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.

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

Examples

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

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.