-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFirstMissingPositive.java
35 lines (30 loc) · 995 Bytes
/
FirstMissingPositive.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
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/first-missing-positive
// T: O(N)
// S: O(1)
public class FirstMissingPositive {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
// Use cycle sort to place positive elements smaller than n at the correct index
for (int i = 0 ; i < n ; ) {
if (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
swap(nums, i, nums[i] - 1);
} else {
i++;
}
}
// Iterate through nums return smallest missing positive integer
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) {
return i + 1;
}
}
// If all elements are at the correct index
// the smallest missing positive number is n + 1
return n + 1;
}
private static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}