Remove Covered Intervals - Problem
Given an array intervals where intervals[i] = [li, ri] represent the interval [li, ri), remove all intervals that are covered by another interval in the list.
An interval [a, b) is covered by interval [c, d) if and only if c ≤ a and b ≤ d.
Return the number of remaining intervals after removing all covered intervals.
Input & Output
Example 1 — Basic Coverage
$
Input:
intervals = [[1,4],[3,6],[2,8]]
›
Output:
2
💡 Note:
Interval [3,6] is covered by [2,8] because 2 ≤ 3 and 6 ≤ 8. So [1,4] and [2,8] remain.
Example 2 — No Coverage
$
Input:
intervals = [[1,4],[2,3]]
›
Output:
2
💡 Note:
Neither interval covers the other: [1,4] doesn't cover [2,3] (4 > 3 but we need 3 ≤ 4), and [2,3] doesn't cover [1,4].
Example 3 — Multiple Covered
$
Input:
intervals = [[1,2],[1,4],[3,4]]
›
Output:
1
💡 Note:
[1,4] covers both [1,2] and [3,4]. Only [1,4] remains.
Constraints
- 1 ≤ intervals.length ≤ 1000
- intervals[i].length == 2
- 0 ≤ li < ri ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of intervals [[1,4],[3,6],[2,8]]
2
Find Coverage
Identify that [3,6] is covered by [2,8]
3
Count Remaining
Return count of uncovered intervals: 2
Key Takeaway
🎯 Key Insight: Sorting by start point enables single-pass detection of covered intervals
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code