2Brute2Best
🔥0

Remove Covered Intervals

Medium·LeetCode #1288 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You get a list of intervals [l, r]. No interval appears twice.

Interval [a, b] is covered by interval [c, d] when it sits completely inside it: c ≤ a and b ≤ d.

Remove every interval that is covered by some other interval. Return how many intervals are left.

Input

A list intervals, where intervals[i] = [l, r].

Output

One integer: the number of intervals that are not covered by any other interval.

Example 1
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2

[3,6] is inside [2,8], so it is removed. [1,4] is not inside [2,8] because it starts earlier. 2 intervals remain.

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

[2,3] is inside [1,4].

Constraints
  • • 1 ≤ intervals.length ≤ 1000
  • • intervals[i].length == 2
  • • 0 ≤ l < r ≤ 10^5
  • • All intervals are different.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“list of intervals”
→ Sort first — by start is the default for this sub-pattern.
“covered: c ≤ a and b ≤ d”
→ A containment test, not an overlap test. Sorting by start handles c ≤ a; a running max handles b ≤ d.
“how many remain”
→ We only count; no need to build the merged list.
03

Approach Complexity Ladder

Brute Force — check every pairO(N²)O(1)

For each interval, look for any other interval that contains it

Optimal — sort by start (ties: end descending), track maxEndO(N log N)O(1)

Sort so earlier intervals start first; an interval survives only if it ends past maxEnd

Brute Force Intuition

For each interval i, loop over every other interval j. If j starts at or before i and ends at or after i, then i is covered. Count the intervals for which no such j exists.

Recognition rule for this sub-pattern:

Sort by start. If next.start ≤ current.end, they overlap — extend current.end. Otherwise start a new interval.
Remember this · Intervals
Trigger
Input is [start, end] pairs; overlap, merge, insert, meeting rooms, free time, minimum removals.
Think
After sorting, do I only ever need to compare against the most recent interval?
Key question
Sort by start or by end — which one makes the greedy choice obvious?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.