Open tsungtingdu opened 3 years ago
var mostCompetitive = function(nums, k) { let steps = nums.length - k let stack = [] for (let i = 0; i < nums.length; i++) { while (stack && nums[i] < stack[stack.length - 1] && steps > 0) { stack.pop() steps-- } stack.push(nums[i]) } return stack.slice(0, k) };