Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 732 Bytes

1673. 找出最具竞争力的子序列.md

File metadata and controls

33 lines (23 loc) · 732 Bytes
function mostCompetitive(nums: number[], k: number): number[] {

    if (k === 0) {
        return [];
    }

    if (k === nums.length) {
        return nums;
    }

    // 把问题转化为要删除多少个数字,可以得到最小子序列
    let removeTimes: number = nums.length - k;
    const stack: number[] = [];

    for (const num of nums) {

        while (stack.length && stack[stack.length - 1] > num && removeTimes) {
            stack.pop();
            --removeTimes;
        }

        stack.push(num);
    }

    return stack.slice(0, stack.length - removeTimes);
};