harrytothemoon / leetcodeAplus

Leetcode meeting note
2 stars 0 forks source link

[1673] Find the Most Competitive Subsequence #90

Open tsungtingdu opened 3 years ago

tsungtingdu commented 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)
};