Find the Most Competitive Subsequence LeetCode solution in Java

1
class Solution {
2
public int[] mostCompetitive(int[] nums, int k) {
3
int[] stack = new int[k];
4
for (int i = 0, j = 0; i < nums.length; i++) {
5
while (j > 0 && stack[j - 1] > nums[i] && j + nums.length - i > k) {
6
j--;
7
}
8
if (j < k) {
9
stack[j++] = nums[i];
10
}
11
}
12
return stack;
13
}
14
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0