-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort Colors.cpp
60 lines (51 loc) · 1.23 KB
/
Sort Colors.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
56
57
58
59
60
// Stupid Method O(n * log(n))
class Solution {
public:
void sortColors(int a[], int n) {
qsort(a, 0, n-1);
}
private:
void _qsort(int a[], int l, int h) {
if (l < h) {
int m = _partition(a, l, h);
_qsort(a, l, m-1);
_qsort(a, m+1, h);
}
}
int _partition(int a[], int l, int h) {
int pvt = a[l];
while (l < h) {
while (l < h && a[h] >= pvt) --h;
a[l] = a[h];
while (l < h && a[l] <= pvt) ++l;
a[h] = a[l];
}
a[l] = pvt;
return l;
}
};
// Smarter version, O(n), constant space;
class Solution {
public:
void sortColors(int a[], int n) {
int mp[3] = {0};
for (int i = 0; i < n; ++i) mp[a[i]] ++;
int idx = 0;
for (int i = 0; i <= 2; ++i) {
while (mp[i]--) a[idx++] = i;
}
}
};
// Much smarter version, O(n), inplace;
class Solution {
public:
void sortColors(int a[], int n) {
int zero = -1, two = n;
int i = 0;
while (i < two) {
if (a[i] == 0) swap(a[i++], a[++zero]);
else if (a[i] == 2) swap(a[i], a[--two]);
else i++;
}
}
};