-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathminimum-operations-to-make-binary-palindrome.cpp
More file actions
56 lines (53 loc) · 1.56 KB
/
minimum-operations-to-make-binary-palindrome.cpp
File metadata and controls
56 lines (53 loc) · 1.56 KB
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
// Time: precompute: O(sqrt(r) * logr + r) = O(sqrt(r) * logr), r = max(nums)
// runtime: O(n)
// Space: O(r)
// precompute, bitmasks, two pointers
const auto& precompute = [](int n) {
const auto& bit_length = [](int x) {
return (x ? std::__lg(x) : -1) + 1;
};
const auto& l = bit_length(n);
vector<int> palindromes;
for (int d = 1; d <= l; ++d) {
const auto& h = (d + 1) / 2;
for (int prefix = (1 << (h - 1)); prefix < (1 << h); ++prefix) {
int p = prefix, t = prefix;
t >>= d % 2;
for (int i = 0; i < h - d % 2; ++i) {
p = (p << 1) | (t & 1);
t >>= 1;
}
if (p <= n) {
palindromes.emplace_back(p);
}
}
}
vector<int> lookup(n + 1, numeric_limits<int>::max());
for (int x = 1, i = 0; x <= n; ++x) {
for (; i < size(palindromes); ++i) {
if (palindromes[i] > x) {
break;
}
}
if (i < size(palindromes)) {
lookup[x] = min(lookup[x], palindromes[i] - x);
}
if (i - 1 >= 0) {
lookup[x] = min(lookup[x], x - palindromes[i - 1]);
}
}
return lookup;
};
const auto& MAX_NUM = 5000;
const auto& LOOKUP = precompute(MAX_NUM);
class Solution {
public:
vector<int> minOperations(vector<int>& nums) {
vector<int> result;
result.reserve(size(nums));
for (const auto& x : nums) {
result.emplace_back(LOOKUP[x]);
}
return result;
}
};