forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial-array-with-x-elements-greater-than-or-equal-x.cpp
133 lines (126 loc) · 3.66 KB
/
special-array-with-x-elements-greater-than-or-equal-x.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Time: O(n)
// Space: O(1)
// counting sort solution
class Solution {
public:
int specialArray(vector<int>& nums) {
static const int MAX_NUM = 1000;
vector<int> count(MAX_NUM + 1);
for (const auto& num : nums) {
++count[num];
}
for (int n = size(nums), i = 0; i < size(count); n -= count[i++]) {
if (i == n) {
return i;
}
}
return -1;
}
};
// Time: O(n)
// Space: O(1)
// binary search + counting sort solution
class Solution2 {
public:
int specialArray(vector<int>& nums) {
inplace_counting_sort(&nums, true);
int left = 0, right = size(nums) - 1;
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (nums[mid] <= mid) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left < size(nums) && nums[left] == left ? -1 : left;
}
private:
void inplace_counting_sort(vector<int> *nums, bool is_reverse) {
static const int MAX_NUM = 1000;
vector<int> count(MAX_NUM + 1);
for (const auto& num : *nums) {
++count[num];
}
for (int i = 1; i < size(count); ++i) {
count[i] += count[i - 1];
}
vector<int> result(size(*nums));
for (int i = size(*nums) - 1; i >= 0; --i) { // inplace but unstable sort
if ((*nums)[i] < 0) { // processed
continue;
}
while (i != count[(*nums)[i]] - 1) {
--count[(*nums)[i]];
tie((*nums)[count[(*nums)[i]]], (*nums)[i]) = pair(~(*nums)[i], (*nums)[count[(*nums)[i]]]);
}
--count[(*nums)[i]];
(*nums)[i] = ~(*nums)[i];
}
for (auto& num : *nums) {
num = ~num; // restore values
}
if (is_reverse) { // unstable sort
reverse(begin(*nums), end(*nums));
}
}
};
// Time: O(n)
// Space: O(n)
// binary search + counting sort solution
class Solution3 {
public:
int specialArray(vector<int>& nums) {
nums = counting_sort(nums, true); // extra O(n) space for stable sort
int left = 0, right = size(nums) - 1;
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (nums[mid] <= mid) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left < size(nums) && nums[left] == left ? -1 : left;
}
private:
vector<int> counting_sort(const vector<int>& nums, bool is_reverse) {
static const int MAX_NUM = 1000;
vector<int> count(MAX_NUM + 1);
for (const auto& num : nums) {
++count[num];
}
for (int i = 1; i < size(count); ++i) {
count[i] += count[i - 1];
}
vector<int> result(size(nums));
if (!is_reverse) {
for (int i = size(nums) - 1; i >= 0; --i) { // stable sort
result[--count[nums[i]]] = nums[i];
}
} else {
for (const auto& num : nums) { // stable sort
result[--count[num]] = num;
}
reverse(begin(result), end(result));
}
return result;
}
};
// Time: O(nlogn)
// Space: O(1)
// sort solution
class Solution4 {
public:
int specialArray(vector<int>& nums) {
sort(rbegin(nums), rend(nums));
int i = 0;
while (i < size(nums)) {
if (nums[i] <= i) {
break;
}
++i;
}
return i < size(nums) && nums[i] == i ? -1 : i;
}
};