forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-number-of-moves-to-make-palindrome.cpp
86 lines (79 loc) · 2.08 KB
/
minimum-number-of-moves-to-make-palindrome.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
// Time: O(nlogn)
// Space: O(n)
// greedy, bit, fenwick tree
class Solution {
public:
int minMovesToMakePalindrome(string s) {
vector<vector<int>> idxs(26);
for (int i = 0; i < size(s); ++i) {
idxs[s[i] - 'a'].emplace_back(i);
}
vector<pair<int, int>> pairs;
vector<int> targets(size(s));
for (const auto& idx : idxs) {
for (int i = 0; i < size(idx) / 2; ++i) {
pairs.emplace_back(idx[i], idx[(size(idx) - 1) - i]);
}
if (size(idx) % 2) {
targets[idx[size(idx) / 2]] = size(s) / 2;
}
}
sort(begin(pairs), end(pairs));
for (int i = 0; i < size(pairs); ++i) {
targets[pairs[i].first] = i;
targets[pairs[i].second] = (size(s) - 1) - i;
}
BIT bit(size(s));
int result = 0;
for (const auto& i : targets) {
result += i - bit.query(i - 1); // move from bit.query(i-1) to i
bit.add(i, 1);
}
return result;
}
private:
class BIT {
public:
BIT(int n) : bit_(n + 1) { // 0-indexed
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
};
// Time: O(n^2)
// Space: O(n)
// greedy
class Solution2 {
public:
int minMovesToMakePalindrome(string s) {
int result = 0;
while (!empty(s)) {
const int i = s.find(s.back());
if (i == size(s) - 1) {
result += i / 2;
} else {
result += i;
s.erase(i, 1);
}
s.pop_back();
}
return result;
}
};