Given an integer array nums and a positive integer k, return _the mostcompetitive subsequence of _numsof sizek.
An array’s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.
We define that a subsequence a is more competitive than a subsequence
b (of the same length) if in the first position where a and b differ, subsequence a has a number less than the corresponding number in b.
For example, [1,3,4] is more competitive than [1,3,5] because the first position they differ is at the final number, and 4 is less than 5.
Input: nums =[3,5,2,6], k =2Output: [2,6]Explanation: Among the set of every possible subsequence:{[3,5],[3,2],[3,6],[5,2],[5,6],[2,6]},[2,6]is the most competitive.
To find the most competitive subsequence of size k, we want the lexicographically smallest possible sequence. We can use a monotonic stack to greedily keep the smallest elements, popping from the stack when a smaller element is found and we still have enough elements left to reach size k.
While the stack is not empty, the current element is smaller than the top of the stack, and there are enough elements left to fill the subsequence, pop the stack.
Push the current element onto the stack.
After processing, the first k elements of the stack form the answer.
classSolution {
publicint[]mostCompetitive(int[] nums, int k) {
int[] stk =newint[k];
int top = 0, n = nums.length;
for(int i=0;i<n;++i) {
while(top>0 && nums[i]<stk[top-1]&& top+n-i-1>=k) top--;
if(top<k) stk[top++]=nums[i];
}
return stk;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funmostCompetitive(nums: IntArray, k: Int): IntArray {
val stk = ArrayList<Int>()
val n = nums.size
for (i in nums.indices) {
while (stk.isNotEmpty() && nums[i] < stk.last() && stk.size + n - i - 1>= k) stk.removeAt(stk.size-1)
if (stk.size < k) stk.add(nums[i])
}
return stk.toIntArray()
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defmostCompetitive(self, nums: list[int], k: int) -> list[int]:
stk = []
n = len(nums)
for i, x in enumerate(nums):
while stk and x < stk[-1] and len(stk) + n - i -1>= k:
stk.pop()
if len(stk) < k:
stk.append(x)
return stk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnmost_competitive(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
letmut stk = Vec::new();
for (i, &x) in nums.iter().enumerate() {
whilelet Some(&last) = stk.last() {
if x < last && stk.len() + n - i -1>= k asusize {
stk.pop();
} else {
break;
}
}
if stk.len() < k asusize {
stk.push(x);
}
}
stk
}
}