- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1673.java
26 lines (24 loc) · 798 Bytes
/
_1673.java
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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.Stack;
publicclass_1673 {
publicstaticclassSolution1 {
publicint[] mostCompetitive(int[] nums, intk) {
Stack<Integer> stack = newStack<>();
for (inti = 0; i < nums.length; i++) {
while (!stack.isEmpty()
&& nums[i] < stack.peek()
&& nums.length - i + stack.size() > k) {
stack.pop();
}
if (stack.size() < k) {
stack.push(nums[i]);
}
}
int[] result = newint[k];
for (inti = k - 1; i >= 0; i--) {
result[i] = stack.pop();
}
returnresult;
}
}
}