Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 759 Bytes

1288. 删除被覆盖区间.md

File metadata and controls

37 lines (24 loc) · 759 Bytes
  • 排序 + 栈
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: 无需 栈,有空再实现