-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1673.java
More file actions
32 lines (29 loc) · 883 Bytes
/
prblm1673.java
File metadata and controls
32 lines (29 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Arrays;
import java.util.Stack;
public class prblm1673 {
public int[] mostCompetitive(int[] nums, int k) {
Stack <Integer> stack = new Stack<>();
int removable = nums.length - k;
for(int i = 0; i < nums.length; i++){
while(!stack.isEmpty() && stack.peek() > nums[i] && removable > 0){
stack.pop();
removable--;
}
stack.push(nums[i]);
}
while(removable > 0){
stack.pop();
removable--;
}
int[] ans = new int[k];
for(int i = k-1; i >= 0; i--){
ans[i] = stack.pop();
}
return ans;
}
public static void main(String[] args) {
int[] nums = {3,5,2,6};
int k = 2;
System.out.println(Arrays.toString(new prblm1673().mostCompetitive(nums, k)));
}
}