Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]] Output: 2 Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]] Output: 1
Constraints:
1 <= intervals.length <= 1000intervals[i].length == 20 <= li < ri <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,4],[3,6],[2,8]]
[[1,4],[2,3]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1288: Remove Covered Intervals
class Solution {
public int removeCoveredIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0, pre = Integer.MIN_VALUE;
for (var e : intervals) {
int cur = e[1];
if (cur > pre) {
++ans;
pre = cur;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1288: Remove Covered Intervals
func removeCoveredIntervals(intervals [][]int) (ans int) {
sort.Slice(intervals, func(i, j int) bool {
if intervals[i][0] == intervals[j][0] {
return intervals[i][1] > intervals[j][1]
}
return intervals[i][0] < intervals[j][0]
})
pre := math.MinInt32
for _, e := range intervals {
cur := e[1]
if cur > pre {
ans++
pre = cur
}
}
return
}
# Accepted solution for LeetCode #1288: Remove Covered Intervals
class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: (x[0], -x[1]))
ans = 0
pre = -inf
for _, cur in intervals:
if cur > pre:
ans += 1
pre = cur
return ans
// Accepted solution for LeetCode #1288: Remove Covered Intervals
struct Solution;
use std::cmp::Reverse;
impl Solution {
fn remove_covered_intervals(mut intervals: Vec<Vec<i32>>) -> i32 {
intervals.sort_by_key(|v| (v[0], Reverse(v[1])));
let n = intervals.len();
let mut r = -1;
let mut res = 0;
for i in 0..n {
let interval = &intervals[i];
if interval[1] <= r {
continue;
} else {
r = interval[1];
res += 1;
}
}
res
}
}
#[test]
fn test() {
let intervals = vec_vec_i32![[1, 4], [3, 6], [2, 8]];
let res = 2;
assert_eq!(Solution::remove_covered_intervals(intervals), res);
}
// Accepted solution for LeetCode #1288: Remove Covered Intervals
function removeCoveredIntervals(intervals: number[][]): number {
intervals.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]));
let ans = 0;
let pre = -Infinity;
for (const [_, cur] of intervals) {
if (cur > pre) {
++ans;
pre = cur;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.