-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
81 lines (71 loc) · 1.97 KB
/
Solution.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package firstmissingpositive;
/**
* Given an unsorted integer array, find the smallest missing positive integer.
*
* Example 1:
*
* Input: [1,2,0]
* Output: 3
* Example 2:
*
* Input: [3,4,-1,1]
* Output: 2
* Example 3:
*
* Input: [7,8,9,11,12]
* Output: 1
* Note:
*
* Your algorithm should run in O(n) time and uses constant extra space.
*/
class Solution {
int firstMissingPositive(int[] nums) {
int n = nums.length;
boolean is1Present = false;
for(int x: nums) {
if(x == 1) {
is1Present = true;
break;
}
}
if(!is1Present)
return 1;
// in case when nums = [1]
if(n == 1)
return 2;
// change -ve elements, 0 and elems > n to 1
for(int i = 0; i < nums.length; i++) {
if(nums[i] <= 0 || nums[i] > n) {
nums[i] = 1;
}
}
// for every elem x, go to nums[x] and change sign to -ve if not already -ve
for(int i = 0; i < nums.length; i++) {
int elem = Math.abs(nums[i]);
// since there is no nth element in array of n elements, store this info in index 0
if(elem == n) {
nums[0] = -n;
}
// otherwise in nums[x] for elem x
else if(nums[elem] > 0) {
nums[elem] = -nums[elem];
}
}
for(int i = 1; i < nums.length; i++) {
// return first index which is +ve
if(nums[i] > 0)
return i;
}
// if all indices from 1 onwards is -ve, check 0th index
if(nums[0] > 0)
return n;
// in case all elems present, first missing is next after n
return n+1;
}
}
/*
Success
Details
Runtime: 0 ms, faster than 100.00% of Java online submissions for First Missing Positive.
Memory Usage: 38 MB, less than 6.85% of Java online submissions for First Missing Positive.
*/