stack
greedy
]
Leetcode 1673. Find the Most Competitive Subsequence
https://leetcode.com/problems/find-the-most-competitive-subsequence
This is a good problem to apply stack, with the following idea: let us traverse original list number by number and put in into stack: if it happens that new number is less than the top of our stack and if we still can afford to delete one more number, we extract it from stack and put new number. Let me illustrate this on example [1,4,5,3,2,8,7]
and k = 4
.
First, we put 1
into stack, then 4
and then 5
, so far we have [1, 4, 5]
. Next step we see number 3
, which is less then 5
, so we keep removing elements from stack until we can and put 3
, so we have [1, 3]
now. Next number is 2
, so we again remove 3
and put 2
, and we have [1, 2]
in our stack now. At this moment number of attempts we can make is equal to zero, so we just must take all the rest numbers, so finally we have [1, 2, 8, 7]
in our stack.
Complexity: both time and space complexity is O(n)
, where n
is length of our string, because for each digit it goes in and goes out of stack only once.
class Solution:
def mostCompetitive(self, nums, k):
attempts = len(nums) - k
stack = []
for num in nums:
while stack and num < stack[-1] and attempts > 0:
stack.pop()
attempts -= 1
stack.append(num)
return stack[:k]
See very similar problem 402. Remove K Digits, where we need to do literally the same.
If you like the solution, you can upvote it on leetcode discussion section: Problem 1673