1673. Find the Most Competitive Subsequence
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 52% Topics: Array, Stack, Greedy, Monotonic Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(k)
class Solution(object):
def mostCompetitive(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
stk = []
for i, x in enumerate(nums):
while stk and stk[-1] > x and len(stk)+(len(nums)-i) > k:
stk.pop()
if len(stk) < k:
stk.append(x)
return stk
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions