stack
to serve as the monotonic stack.additionalCount
as the number of elements we can drop, which is len(nums) - k
.nums
:
a. While stack
is not empty and the last element of stack
is greater than the current element and additionalCount
is greater than 0, pop the last element from stack
and decrement additionalCount
.
b. If the length of stack
is less than k
, append the current element to stack
.k
elements of stack
as they form the most competitive subsequence.