- 排序 + 栈
function removeCoveredIntervals(intervals: number[][]): number {
// sort intervals
intervals.sort((a, b) => b[0] - a[0] || a[1] - b[1]);
const stack: [number, number][] = [];
for (const [left, right] of intervals) {
while (stack.length && isCover(stack[stack.length - 1], [left, right])) {
stack.pop();
}
stack.push([left, right]);
}
return stack.length;
};
function isCover(child: [number, number], parent: [number, number]): boolean {
return child[0] >= parent[0]
&& child[1] <= parent[1];
}
- 排序
// TODO: 无需 栈,有空再实现