-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstMissingPositive.cpp
34 lines (34 loc) · 1012 Bytes
/
FirstMissingPositive.cpp
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
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
const int INF = nums.size()+1;
// Dummy value. [1,2,3] -> need to be able to map 3!
nums.push_back(INF);
for (int& num : nums) {
if (num < 0 || num >= nums.size()) {
num = INF;
}
}
for (int i = 0; i < nums.size(); ++i) {
int num = abs(nums[i]);
if (num == INF) {
continue;
} else if (nums[num] == 0) {
// Can't mark this negative.
nums[num] = -INF;
} else {
nums[num] = min(nums[num], -nums[num]);
}
}
int ans = INF;
// No need to check if zero exists.
for (int i = 1; i < nums.size(); ++i) {
// We marked nums[i] as negative if i existed in nums.
if (nums[i] >= 0) {
ans = i;
break;
}
}
return ans;
}
};