Skip to content

Commit c80e033

Browse files
committed
add firstMissingPositive.cpp
1 parent ab1355c commit c80e033

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

C++/firstMissingPositive.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
class Solution {
5+
public:
6+
int firstMissingPositive(int A[], int n) {
7+
int i;
8+
bucketSort(A, n);
9+
for(i = 0; i < n && A[i] == i + 1; ++i);
10+
return i + 1;
11+
}
12+
13+
private:
14+
void bucketSort(int A[], int n) {
15+
for(int i = 0; i < n; ++i) {
16+
for (; A[i] != i + 1 && A[i] > 0 && A[i] <= n && A[i] != A[A[i] - 1];) {
17+
swap(A[i], A[A[i] - 1]);
18+
}
19+
}
20+
}
21+
};

0 commit comments

Comments
 (0)