Remove Covered Intervals

Medium Topics Company Tags

Each distinct entry [left, right] in intervals represents the half-open interval [left, right). Remove an interval when another single interval starts at or before its left endpoint and ends at or after its right endpoint. Return how many intervals remain. Partial overlap alone is not enough, and coverage must come from one other interval rather than the union of several intervals.

Example 1:

Input: intervals = [[0,8],[2,5],[6,10]]

Output: 2

Explanation: The interval [2,5) lies inside [0,8) and is removed. Neither [0,8) nor [6,10) covers the other, leaving 2 intervals.

Example 2:

Input: intervals = [[1,3],[1,7],[4,7]]

Output: 1

Explanation: The interval [1,7) covers both [1,3) and [4,7). Equal endpoints are allowed in the containment condition, so only [1,7) remains.

Example 3:

Input: intervals = [[0,4],[3,7],[6,9]]

Output: 3

Explanation: The intervals overlap, but none is fully contained in a single other interval. All 3 remain.

Constraints:

  • 1 <= intervals.length <= 1000
  • Every interval contains exactly two integers.
  • 0 <= left < right <= 100000
  • All intervals are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.