forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1673.java
More file actions
24 lines (22 loc) · 735 Bytes
/
_1673.java
File metadata and controls
24 lines (22 loc) · 735 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
package com.fishercoder.solutions;
import java.util.Stack;
public class _1673 {
public static class Solution1 {
public int[] mostCompetitive(int[] nums, int k) {
Stack<Integer> stack = new Stack<>();
for (int i = 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 = new int[k];
for (int i = k - 1; i >= 0; i--) {
result[i] = stack.pop();
}
return result;
}
}
}