Skip to main content
Back to problems
#1288
Medium Algorithms

Remove covered intervals

Array Sorting
56.1% acceptance
Feb 25, 2026
2312
60
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. The interval [a, b) is covered by the interval [c, d) if and only if c <= a and b <= d. Return the number of remaining intervals.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn remove_covered_intervals(mut intervals: Vec<Vec<i32>>) -> i32 {
    // Sort by start ascending, then by end descending
    intervals.sort_by(|a, b| a[0].cmp(&b[0]).then(b[1].cmp(&a[1])));
    let mut count = 0;
    let mut max_end = 0;
    for interval in &intervals {
      if interval[1] > max_end {
        count += 1;
        max_end = interval[1];
      }
    }
    count
  }
}