-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirst Missing Positive.cpp
More file actions
45 lines (45 loc) · 965 Bytes
/
Copy pathFirst Missing Positive.cpp
File metadata and controls
45 lines (45 loc) · 965 Bytes
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
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n=nums.size();
int m=INT_MIN;
for(int i:nums)
m=max(m,i);
if(m>n)
{
int hash[n+1];
for(int i=0;i<n+1;i++)
hash[i]=0;
for(int i:nums)
{
if(i<n+1 && i>=0)
{
hash[i]++;
}
}
for(int i=1;i<n+1;i++)
{
if(hash[i]==0)
return i;
}
}
else
{
int hash[n+1];
for(int i=0;i<n+1;i++)
hash[i]=0;
for(int i:nums)
{
if(i>=0)
hash[i]++;
}
for(int i=1;i<n+1;i++)
{
if(hash[i]==0)
return i;
}
return n+1;
}
return -1;
}
};