MEDIUM NC#132 Blind #37 Intervals / Greedy
435. 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.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
- โขApply Greedy reasoning pattern
๐ก Approach
- โ Sort intervals by end time
- โ Count non-overlapping intervals greedily
- โ If current start < last end, they overlap
- โ Time: O(n log n), Space: O(1)
๐ ๏ธ Hints & Pitfalls
Hints
- โขSort intervals by end time
- โขCount non-overlapping intervals greedily
- โขIf current start < last end, they overlap
Common Pitfalls
- โขTime: O(n log n), Space: O(1)
๐งช Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
eraseOverlapIntervals([[1,2],[2,3],[3,4],[1,3]]); Expected:
1 Test Case 2
Not run Input:
eraseOverlapIntervals([[1,2],[1,2],[1,2]]); Expected:
2 Test Case 3
Not run Input:
eraseOverlapIntervals([[1,2],[2,3]]); Expected:
0 ๐ Code Editor
๐ค Output