Remove Covered Intervals

Given an array `intervals` where `intervals[i] = [l_i, r_i]` represents the interval `[l_i, r_i)`, 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*.

30:00

Remove Covered Intervals
medium
Free written solution
Topics
Companies

Given an array intervals where intervals[i] = [l_i, r_i] represents the interval [l_i, r_i), 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.

Example 1:
Input: {"intervals":[[1,4],[3,6],[2,8]]}
Output: 2
Constraints:
  • 1≤intervals.length≤10001 \leq \text{intervals.length} \leq 1000

  • intervals[i].length=2\text{intervals}[i].\text{length} = 2

  • 0≤li<ri≤1050 \leq l_i < r_i \leq 10^5

  • All the given intervals are unique.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"intervals":[[1,4],[3,6],[2,8]]}

3 intervals. An interval is covered when another one starts no later and ends no earlier. Count the ones that survive.

input order

[1,4]

[3,6]

[2,8]

18

reach

-

kept

0

covered

0

checking now
kept
covered, removed
furthest end so far
Variables
VariableValue
interval-
reach0
kept0
DepthFunction Call
Stack empty
0/8