Skip to content

Commit ce5ca27

Browse files
authored
Create find-the-most-competitive-subsequence.py
1 parent 9e85319 commit ce5ca27

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(n)
2+
# Space: O(k)
3+
4+
class Solution(object):
5+
def mostCompetitive(self, nums, k):
6+
"""
7+
:type nums: List[int]
8+
:type k: int
9+
:rtype: List[int]
10+
"""
11+
stk = []
12+
for i, x in enumerate(nums):
13+
while stk and stk[-1] > x and len(stk)+(len(nums)-i) > k:
14+
stk.pop()
15+
if len(stk) < k:
16+
stk.append(x)
17+
return stk

0 commit comments

Comments
 (0)