1414*
1515*
1616**********************************************************************************/
17-
17+ #include < stdlib.h>
18+ #include < time.h>
1819#include < iostream>
1920#include < map>
2021using namespace std ;
2122
2223#define INT_MAX 2147483647
2324
2425/*
25- * The idea is simple:
26+ * Idea:
27+ *
28+ * We can move the num to the place whcih the index is the num.
29+ *
30+ * for example, (considering the array is zero-based.
31+ * 1 => A[0], 2 => A[1], 3=>A[2]
32+ *
33+ * Then, we can go through the array check the i+1 == A[i], if not ,just return i+1;
34+ *
35+ * This solution comes from StackOverflow.com
36+ * http://stackoverflow.com/questions/1586858/find-the-smallest-integer-not-in-a-list
37+ */
38+ int firstMissingPositive_move (int A[], int n) {
39+ if (n<=0 ) return 1 ;
40+ int num;
41+ for (int i=0 ; i<n; i++) {
42+ num = A[i];
43+ while (num>0 && num<n && A[num-1 ]!=num) {
44+ swap (A[i], A[num-1 ]);
45+ num = A[i];
46+ }
47+ }
48+ for (int i=0 ; i<n; i++){
49+ if (i+1 != A[i]){
50+ return i+1 ;
51+ }
52+ }
53+ return n+1 ;
54+ }
55+
56+ /*
57+ * The idea is simple:
2658 *
2759 * 1) put all of number into a map.
2860 * 2) for each number a[i] in array, remove its continous number in the map
@@ -36,8 +68,10 @@ using namespace std;
3668 *
3769 * However, we missed 1, so, we have to add dummy number 0 whatever.
3870 *
71+ * NOTE: this solution is not constant space slution!!!!
72+ *
3973 */
40- int firstMissingPositive (int A[], int n) {
74+ int firstMissingPositive_map (int A[], int n) {
4175 map<int , int > cache;
4276 for (int i=0 ; i<n; i++){
4377 cache[A[i]] = i;
@@ -51,13 +85,13 @@ int firstMissingPositive(int A[], int n) {
5185 int miss = INT_MAX;
5286 int x;
5387 for (int i=-1 ; i<n && cache.size ()>0 ; i++){
54-
88+
5589 if (i == -1 ){
5690 x = 0 ; // checking dummy
5791 }else {
5892 x = A[i];
5993 }
60-
94+
6195 if ( cache.find (x)==cache.end () ){
6296 continue ;
6397 }
@@ -78,32 +112,66 @@ int firstMissingPositive(int A[], int n) {
78112 miss = num;
79113 }
80114 }
81-
115+
82116
83117 return miss;
84118}
85119
120+ int firstMissingPositive (int A[], int n) {
121+ srand (time (0 ));
122+ if (rand ()%2 ){
123+ return firstMissingPositive_move (A, n);
124+ }
125+ return firstMissingPositive_map (A, n);
126+ }
127+
128+
129+ void printArray (int a[], int n){
130+ cout << " [ " ;
131+ for (int i=0 ; i<n-1 ; i++) {
132+ cout << a[i] << " , " ;
133+ }
134+ cout << a[n-1 ] << " ]" ;
135+ }
136+
137+ void Test (int a[], int n, int expected) {
138+ printArray (a, n);
139+ int ret = firstMissingPositive (a, n);
140+ cout << " \t missed = " << ret << " " << (ret==expected?" passed!" :" failed!" ) << endl;
141+ // printArray(a, n);
142+ // cout <<endl;
143+ }
144+
86145int main ()
87146{
88- #define TEST (a ) cout << firstMissingPositive(a, sizeof (a)/sizeof (int )) << endl
147+ #define TEST (a, e ) Test(a, sizeof (a)/sizeof (int ), e)
148+
149+ int a0[]={1 };
150+ TEST (a0, 2 );
89151
90152 int a1[]={1 ,2 ,0 };
91- TEST (a1);
153+ TEST (a1, 3 );
92154
93155 int a2[]={3 ,4 ,-1 ,1 };
94- TEST (a2);
156+ TEST (a2, 2 );
95157
96158 int a3[]={1000 ,-1 };
97- TEST (a3);
159+ TEST (a3, 1 );
98160
99161 int a4[]={1000 , 200 };
100- TEST (a4);
162+ TEST (a4, 1 );
101163
102- int a5[]={2 ,5 ,4 , 3 ,-1 };
103- TEST (a5);
164+ int a5[]={2 ,5 ,3 ,-1 };
165+ TEST (a5, 1 );
104166
105167 int a6[]={1 , 100 };
106- TEST (a6);
168+ TEST (a6, 2 );
169+
170+ int a7[]={7 ,8 ,9 ,11 };
171+ TEST (a7, 1 );
172+
173+ int a8[]={4 ,3 ,2 ,1 };
174+ TEST (a8, 5 );
107175
108176 return 0 ;
109177}
0 commit comments