Skip to content

Commit

Permalink
add O(1) space solution for "First Missing Positive" problem. fixed #19
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Nov 20, 2014
1 parent 0d1d25f commit 8711a9a
Showing 1 changed file with 82 additions and 14 deletions.
96 changes: 82 additions & 14 deletions src/firstMissingPositive/firstMissingPositive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,47 @@
*
*
**********************************************************************************/

#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <map>
using namespace std;

#define INT_MAX 2147483647

/*
* The idea is simple:
* Idea:
*
* We can move the num to the place whcih the index is the num.
*
* for example, (considering the array is zero-based.
* 1 => A[0], 2 => A[1], 3=>A[2]
*
* Then, we can go through the array check the i+1 == A[i], if not ,just return i+1;
*
* This solution comes from StackOverflow.com
* http://stackoverflow.com/questions/1586858/find-the-smallest-integer-not-in-a-list
*/
int firstMissingPositive_move(int A[], int n) {
if (n<=0) return 1;
int num;
for(int i=0; i<n; i++) {
num = A[i];
while (num>0 && num<n && A[num-1]!=num) {
swap(A[i], A[num-1]);
num = A[i];
}
}
for (int i=0; i<n; i++){
if (i+1 != A[i]){
return i+1;
}
}
return n+1;
}

/*
* The idea is simple:
*
* 1) put all of number into a map.
* 2) for each number a[i] in array, remove its continous number in the map
Expand All @@ -36,8 +68,10 @@ using namespace std;
*
* However, we missed 1, so, we have to add dummy number 0 whatever.
*
* NOTE: this solution is not constant space slution!!!!
*
*/
int firstMissingPositive(int A[], int n) {
int firstMissingPositive_map(int A[], int n) {
map<int, int> cache;
for(int i=0; i<n; i++){
cache[A[i]] = i;
Expand All @@ -51,13 +85,13 @@ int firstMissingPositive(int A[], int n) {
int miss = INT_MAX;
int x;
for (int i=-1; i<n && cache.size()>0; i++){

if (i == -1){
x = 0; //checking dummy
}else{
x = A[i];
}

if ( cache.find(x)==cache.end() ){
continue;
}
Expand All @@ -78,32 +112,66 @@ int firstMissingPositive(int A[], int n) {
miss = num;
}
}


return miss;
}

int firstMissingPositive(int A[], int n) {
srand(time(0));
if (rand()%2){
return firstMissingPositive_move(A, n);
}
return firstMissingPositive_map(A, n);
}


void printArray(int a[], int n){
cout << "[ ";
for(int i=0; i<n-1; i++) {
cout << a[i] << ", ";
}
cout << a[n-1] << " ]";
}

void Test(int a[], int n, int expected) {
printArray(a, n);
int ret = firstMissingPositive(a, n);
cout << "\t missed = " << ret << " " << (ret==expected?"passed!":"failed!") << endl;
//printArray(a, n);
//cout <<endl;
}

int main()
{
#define TEST(a) cout << firstMissingPositive(a, sizeof(a)/sizeof(int)) << endl
#define TEST(a, e) Test(a, sizeof(a)/sizeof(int), e)

int a0[]={1};
TEST(a0, 2);

int a1[]={1,2,0};
TEST(a1);
TEST(a1, 3);

int a2[]={3,4,-1,1};
TEST(a2);
TEST(a2, 2);

int a3[]={1000,-1};
TEST(a3);
TEST(a3, 1);

int a4[]={1000, 200};
TEST(a4);
TEST(a4, 1);

int a5[]={2,5,4,3,-1};
TEST(a5);
int a5[]={2,5,3,-1};
TEST(a5, 1);

int a6[]={1, 100};
TEST(a6);
TEST(a6, 2);

int a7[]={7,8,9,11};
TEST(a7, 1);

int a8[]={4,3,2,1};
TEST(a8, 5);

return 0;
}

0 comments on commit 8711a9a

Please sign in to comment.