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