- 栈
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);
};