Skip to content

Commit 4329671

Browse files
committed
Add the solution for finding 'First Missing Positive' number
1 parent 0846060 commit 4329671

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.sean.array;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/***
7+
* 41. First Missing Positive
8+
* https://leetcode.com/problems/first-missing-positive/
9+
*/
10+
public class MissingPositiveFinder {
11+
public int firstMissingPositive(int[] nums) { // O(N) time | O(1) space
12+
if (nums == null || nums.length == 0) return 1;
13+
14+
boolean found = false;
15+
for (int e : nums) {
16+
if (e == 1) {
17+
found = true;
18+
break;
19+
}
20+
}
21+
if (!found) {
22+
return 1;
23+
}
24+
25+
// 1. separate the negatives and zeroes
26+
int i = 0, j = nums.length - 1;
27+
while (i < j) {
28+
while (i < nums.length && nums[i] > 0) i++;
29+
while (j >= 0 && nums[j] <= 0) j--;
30+
31+
if (i >= nums.length || j < 0 || i >= j) break;
32+
33+
// swap it
34+
swap(nums, i, j);
35+
36+
i++;
37+
}
38+
39+
int lastPos = -1;
40+
for (int n = 0; n < nums.length; n++) {
41+
if (nums[n] <= 0) {
42+
lastPos = n - 1;
43+
break;
44+
}
45+
}
46+
if (lastPos == -1) lastPos = nums.length - 1;
47+
48+
// 2. mark all the present orders
49+
for (int k = 0; k <= lastPos; k++) {
50+
int index = Math.abs(nums[k]);
51+
if (index > 0 && index <= nums.length && nums[index - 1] > 0) { // nums[k] > 0
52+
nums[index - 1] = -nums[index - 1];
53+
}
54+
}
55+
for (int m = 0; m <= lastPos; m++) {
56+
if (nums[m] > 0) {
57+
return m + 1;
58+
}
59+
}
60+
61+
// all located : return size + 1 [lastPos + 1] + 1
62+
return lastPos + 2;
63+
}
64+
65+
private void swap(int[] nums, int i, int j) {
66+
int t = nums[i];
67+
nums[i] = nums[j];
68+
nums[j] = t;
69+
}
70+
71+
public int firstMissingPositive0(int[] nums) { // O(N) time | O(1) space
72+
if (nums == null || nums.length == 0) return 1;
73+
74+
Set<Integer> set = new HashSet<>();
75+
76+
for (int i : nums) {
77+
if (i > 0) {
78+
set.add(i);
79+
}
80+
}
81+
82+
for (int k = 1; k <= nums.length + 1; k++) {
83+
if (!set.contains(k)) {
84+
return k;
85+
}
86+
}
87+
88+
return 1;
89+
}
90+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class MissingPositiveFinderTest {
9+
10+
private MissingPositiveFinder finder;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
finder = new MissingPositiveFinder();
15+
}
16+
17+
@Test
18+
public void firstMissingPositive() {
19+
assertEquals(finder.firstMissingPositive(new int[] {3, 4, -1, 1}), 2);
20+
assertEquals(finder.firstMissingPositive(new int[] {1, 2, 0}), 3);
21+
assertEquals(finder.firstMissingPositive(new int[] {1, 1}), 2);
22+
assertEquals(finder.firstMissingPositive(new int[] {7,8,9,11,12}), 1);
23+
}
24+
}

0 commit comments

Comments
 (0)