Skip to content

Commit 0430ec7

Browse files
add 1673
1 parent c64edc7 commit 0430ec7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1673|[Find the Most Competitive Subsequence](https://leetcode.com/problems/find-the-most-competitive-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1673.java) ||Medium|Stack, Greedy|
1112
|1672|[Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1672.java) ||Easy|Array|
1213
|1669|[Merge In Between Linked Lists](https://leetcode.com/problems/merge-in-between-linked-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1669.java) ||Medium|LinedList|
1314
|1668|[Maximum Repeating Substring](https://leetcode.com/problems/maximum-repeating-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1668.java) ||Easy|String|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
public class _1673 {
6+
public static class Solution1 {
7+
public int[] mostCompetitive(int[] nums, int k) {
8+
Stack<Integer> stack = new Stack<>();
9+
for (int i = 0; i < nums.length; i++) {
10+
while (!stack.isEmpty() && nums[i] < stack.peek() && nums.length - i + stack.size() > k) {
11+
stack.pop();
12+
}
13+
if (stack.size() < k) {
14+
stack.push(nums[i]);
15+
}
16+
}
17+
int[] result = new int[k];
18+
for (int i = k - 1; i >= 0; i--) {
19+
result[i] = stack.pop();
20+
}
21+
return result;
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1673;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1673Test {
10+
private static _1673.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1673.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{2, 6}, solution1.mostCompetitive(new int[]{3, 5, 2, 6}, 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{2, 3, 3, 4}, solution1.mostCompetitive(new int[]{2, 4, 3, 3, 5, 4, 9, 6}, 4));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)