Skip to content

Commit 03a832a

Browse files
add 1539
1 parent 4e16197 commit 03a832a

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

+1
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+
|1539|[Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1539.java) | |Medium|Array, HashTable|
1112
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1535.java) | [:tv:](https://youtu.be/v6On1TQfMTU) |Medium|Array|
1213
|1534|[Count Good Triplets](https://leetcode.com/problems/count-good-triplets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1534.java) | |Easy|Array|
1314
|1528|[Shuffle String](https://leetcode.com/problems/shuffle-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1528.java) | |Easy|Sort|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _1539 {
7+
public static class Solution1 {
8+
/**
9+
* Space: O(n)
10+
* Time: O(n)
11+
*/
12+
public int findKthPositive(int[] arr, int k) {
13+
Set<Integer> set = new HashSet<>();
14+
int max = 0;
15+
for (int i : arr) {
16+
set.add(i);
17+
max = Math.max(max, i);
18+
}
19+
int missed = 0;
20+
for (int i = 1; i <= max; i++) {
21+
if (!set.contains(i)) {
22+
missed++;
23+
}
24+
if (missed == k) {
25+
return i;
26+
}
27+
}
28+
while (missed++ < k) {
29+
max++;
30+
}
31+
return max;
32+
}
33+
}
34+
35+
public static class Solution2 {
36+
/**
37+
* Space: O(1)
38+
* Time: O(n)
39+
*/
40+
public int findKthPositive(int[] arr, int k) {
41+
int missed = 0;
42+
for (int i = 0; i < arr.length; i++) {
43+
if (i == 0) {
44+
missed += arr[0] - 1;
45+
if (missed >= k) {
46+
return k;
47+
}
48+
} else {
49+
missed += arr[i] - arr[i - 1] - 1;
50+
if (missed >= k) {
51+
missed -= arr[i] - arr[i - 1] - 1;
52+
int result = arr[i - 1];
53+
while (missed++ < k) {
54+
result++;
55+
}
56+
return result;
57+
}
58+
}
59+
}
60+
int result = arr[arr.length - 1];
61+
while (missed++ < k) {
62+
result++;
63+
}
64+
return result;
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1539;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1539Test {
10+
private static _1539.Solution1 solution1;
11+
private static _1539.Solution2 solution2;
12+
private static int[] arr;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1539.Solution1();
17+
solution2 = new _1539.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
arr = new int[]{2, 3, 4, 7, 11};
23+
assertEquals(9, solution1.findKthPositive(arr, 5));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
arr = new int[]{1, 2, 3, 4};
29+
assertEquals(6, solution1.findKthPositive(arr, 2));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
arr = new int[]{2, 3, 4, 7, 11};
35+
assertEquals(9, solution2.findKthPositive(arr, 5));
36+
}
37+
38+
@Test
39+
public void test4() {
40+
arr = new int[]{1, 2, 3, 4};
41+
assertEquals(6, solution2.findKthPositive(arr, 2));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)