Find the Most Competitive Subsequence LeetCode solution in JavaScript

1
var mostCompetitive = function (nums, k) {
2
let nbToRemove = nums.length - k,
3
stack = [];
4
for (const num of nums) {
5
while (num < stack[stack.length - 1] && stack.length && nbToRemove) {
6
stack.pop();
7
nbToRemove--;
8
}
9
stack.push(num);
10
}
11
while (nbToRemove) {
12
stack.pop();
13
nbToRemove--;
14
}
15
return stack;
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0