Leetcode 1792 Solution

This article provides solution to leetcode question 1792 (find-the-most-competitive-subsequence)

https://leetcode.com/problems/find-the-most-competitive-subsequence

Solution

class Solution:
    def mostCompetitive(self, nums: List[int], k: int) -> List[int]:
        ans = []

        for i, num in enumerate(nums):
            left_nums = len(nums) - i

            while len(ans) + left_nums > k and len(ans) > 0 and ans[-1] > num:
                ans.pop()

            if len(ans) < k:
                ans.append(num)

        return ans