-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
final-array-state-after-k-multiplication-operations-i.cpp
207 lines (198 loc) · 6.47 KB
/
final-array-state-after-k-multiplication-operations-i.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Time: O(n + (n + logr) + nlog(logr) + nlogn) = O(nlogn), assumed log(x) takes O(1) time
// Space: O(n)
// sort, two pointers, sliding window, fast exponentiation
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
static const double EPS = 1e-15;
const auto& count = [](const auto& x, int target) {
return static_cast<int>(target - x + EPS);
};
if (multiplier == 1) {
return nums;
}
using P = pair<double, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(log(nums[i]) / log(multiplier), i);
}
sort(begin(vals), end(vals));
int right = 1;
for (int left = 0; right <= static_cast<int>(vals.back().first) + 1; ++right) {
for (; left < size(vals) && count(vals[left].first, right) >= 1; ++left);
if (k - left < 0) {
--right;
break;
}
k -= left;
}
if (right == static_cast<int>(vals.back().first) + 2) {
--right;
}
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
const int c = count(x, right);
if (c <= 0) {
break;
}
nums[i] *= pow(multiplier, c);
}
vals.clear();
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(nums[i], i);
}
sort(begin(vals), end(vals));
const int q = k / size(nums), r = k % size(nums);
const int m = pow(multiplier, q);
vector<int> result(size(nums));
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
result[i] = x * m * (idx < r ? multiplier : 1);
}
return result;
}
};
// Time: O(n + min(n, k) * log(logr) + nlog(logr) + nlogn) = O(nlogr), assumed log(x) takes O(1) time
// Space: O(n)
// binary search, sort, fast exponentiation
class Solution2 {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
static const double EPS = 1e-15;
const auto& binary_search_right = [](auto left, auto right, const auto& check) {
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
};
const auto& count = [](const auto& x, int target) {
return static_cast<int>(target - x + EPS);
};
if (multiplier == 1) {
return nums;
}
using P = pair<double, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(log(nums[i]) / log(multiplier), i);
}
sort(begin(vals), end(vals));
const auto& check = [&](const auto& target) {
int result = 0;
for (const auto& [x, i] : vals) {
const int c = count(x, target);
if (c <= 0) {
break;
}
result += c;
}
return result <= k;
};
const int target = binary_search_right(1, static_cast<int>(vals.back().first) + 1, check);
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
const int c = count(x, target);
if (c <= 0) {
break;
}
k -= c;
nums[i] *= pow(multiplier, c);
}
vals.clear();
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(nums[i], i);
}
sort(begin(vals), end(vals));
const int q = k / size(nums), r = k % size(nums);
const int m = pow(multiplier, q);
vector<int> result(size(nums));
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
result[i] = x * m * (idx < r ? multiplier : 1);
}
return result;
}
};
// Time: O(min(nlogr, k) * logn + nlogn) = O(nlogn * logr)
// Space: O(n)
// heap, sort, fast exponentiation
class Solution3 {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
if (multiplier == 1) {
return nums;
}
using P = pair<int, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(nums[i], i);
}
priority_queue<P, vector<P>, greater<P>> min_heap(cbegin(vals), cend(vals));
const int mx = ranges::max(nums);
for (; k; --k) {
const auto [x, i] = min_heap.top(); min_heap.pop();
if (x >= mx) {
break;
}
nums[i] *= multiplier;
min_heap.emplace(nums[i], i);
}
vals.clear();
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(nums[i], i);
}
sort(begin(vals), end(vals));
const int q = k / size(nums), r = k % size(nums);
const int m = pow(multiplier, q);
vector<int> result(size(nums));
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
result[i] = x * m * (idx < r ? multiplier : 1);
}
return result;
}
};
// Time: O(n + klogn)
// Space: O(n)
// simulation, heap
class Solution4 {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
if (multiplier == 1) {
return nums;
}
using P = pair<int, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(nums[i], i);
}
priority_queue<P, vector<P>, greater<P>> min_heap(cbegin(vals), cend(vals));
for (int _ = 0; _ < k; ++_) {
const auto [x, i] = min_heap.top(); min_heap.pop();
nums[i] *= multiplier;
min_heap.emplace(nums[i], i);
}
return nums;
}
};
// Time: O(k * n)
// Space: O(1)
// simulation
class Solution5 {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
if (multiplier == 1) {
return nums;
}
for (int _ = 0; _ < k; ++_) {
const int i = distance(cbegin(nums), min_element(cbegin(nums), cend(nums)));
nums[i] *= multiplier;
}
return nums;
}
};