-
Notifications
You must be signed in to change notification settings - Fork 5
/
day-183.cpp
55 lines (40 loc) · 1.08 KB
/
day-183.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
First Missing Positive
Solution
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
Follow up:
Your algorithm should run in O(n) time and uses constant extra space.
*/
// Solved using hashMap and single iteration
// beats 83% cpp solution
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N = nums.size();
unordered_map<int, int> hashMap;
int minVal = INT_MAX;
int maxVal = INT_MIN;
for (int idx = 0; idx < N; idx++) {
hashMap[nums[idx]] += 1;
minVal = min(minVal, nums[idx]);
maxVal = max(maxVal, nums[idx]);
}
if (minVal <= 0) minVal = 1;
if (minVal > 1) return 1;
for (int num = minVal; num <= maxVal; num++) {
if (num > 0 && hashMap[num] == 0) return num;
}
return (maxVal < 0 ? 1 : maxVal + 1);
}
};