forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-strong-pair-xor-i.cpp
203 lines (186 loc) · 5.71 KB
/
maximum-strong-pair-xor-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
// Time: O(nlogn + nlogr) = O(nlogr), r = max(nums)
// Space: O(t)
// bit manipulation, greedy, trie, sort, two pointers
class Solution {
private:
class Trie {
public:
Trie(int bit_length)
: bit_length_(bit_length)
, nodes_() {
new_node();
}
void update(int num, int d) {
int curr = 0;
for (int i = bit_length_ - 1; i >= 0; --i) {
int x = num >> i;
if (nodes_[curr][x & 1] == -1) {
nodes_[curr][x & 1] = new_node();
}
curr = nodes_[curr][x & 1];
cnts_[curr] += d;
}
}
int query(int num) {
int result = 0, curr = 0;
for (int i = bit_length_ - 1; i >= 0; --i) {
result <<= 1;
const int x = num >> i;
if (nodes_[curr][1 ^ (x & 1)] != -1 && cnts_[nodes_[curr][1 ^ (x & 1)]]) {
result |= 1;
curr = nodes_[curr][1 ^ (x & 1)];
} else {
curr = nodes_[curr][x & 1];
}
}
return result;
}
private:
int new_node() {
nodes_.push_back(array<int, 2>{-1, -1});
cnts_.emplace_back(0);
return size(nodes_) - 1;
}
const int bit_length_;
vector<array<int, 2>> nodes_;
vector<int> cnts_;
};
public:
int maximumStrongPairXor(vector<int>& nums) {
sort(begin(nums), end(nums));
Trie trie(bit_length(nums.back()));
int result = 0;
for (int i = 0, j = 0; i < size(nums); ++i) {
trie.update(nums[i], +1);
while (!(nums[i] <= 2 * nums[j])) {
trie.update(nums[j++], -1);
}
result = max(result, trie.query(nums[i]));
}
return result;
}
private:
int bit_length(int x) {
return (x ? std::__lg(x) : -1) + 1;
}
};
// Time: O(nlogr), r = max(nums)
// Space: O(t)
// bit manipulation, greedy, trie, dp
class Solution2 {
private:
class Trie {
public:
Trie(int bit_length)
: bit_length_(bit_length)
, nodes_() {
new_node();
}
void insert(int num) {
int curr = 0;
for (int i = bit_length_ - 1; i >= 0; --i) {
int x = num >> i;
if (nodes_[curr][x & 1] == -1) {
nodes_[curr][x & 1] = new_node();
}
curr = nodes_[curr][x & 1];
mins_[curr] = min(mins_[curr], num);
maxs_[curr] = max(maxs_[curr], num);
}
}
int query(int num) {
int result = 0, curr = 0;
for (int i = bit_length_ - 1; i >= 0; --i) {
result <<= 1;
const int x = num >> i;
const int y = (result | 1) ^ x;
assert(x != y);
if (nodes_[curr][y & 1] != -1 &&
((x > y && num <= 2 * maxs_[nodes_[curr][y & 1]]) ||
(x < y && mins_[nodes_[curr][y & 1]] <= 2 * num))) {
result |= 1;
curr = nodes_[curr][y & 1];
} else {
curr = nodes_[curr][(y & 1) ^ 1];
}
}
return result;
}
private:
int new_node() {
nodes_.push_back(array<int, 2>{-1, -1});
mins_.emplace_back(numeric_limits<int>::max());
maxs_.emplace_back(numeric_limits<int>::min());
return size(nodes_) - 1;
}
const int bit_length_;
vector<array<int, 2>> nodes_;
vector<int> mins_;
vector<int> maxs_;
};
public:
int maximumStrongPairXor(vector<int>& nums) {
Trie trie(bit_length(*max_element(cbegin(nums), cend(nums))));
int result = 0;
for (const auto& num : nums) {
trie.insert(num);
result = max(result, trie.query(num));
}
return result;
}
private:
int bit_length(int x) {
return (x ? std::__lg(x) : -1) + 1;
}
};
// Time: O(nlogr), r = max(nums)
// Space: O(n)
// bit manipulation, greedy, dp
class Solution3 {
public:
int maximumStrongPairXor(vector<int>& nums) {
int result = 0;
for (int i = bit_length(*max_element(cbegin(nums), cend(nums))) - 1; i >= 0; --i) {
unordered_map<int, int> prefix_min, prefix_max;
for (const auto& x : nums) {
const int y = x >> i;
if (!prefix_min.count(y)) {
prefix_min[y] = prefix_max[y] = x;
}
prefix_min[y] = min(prefix_min[y], x);
prefix_max[y] = max(prefix_max[y], x);
}
result <<= 1;
for (const auto& [x, _] : prefix_min) {
const int y = (result | 1) ^ x;
assert(x != y);
if (prefix_max.count(y) && prefix_min[max(x, y)] <= 2 * prefix_max[min(x, y)]) {
result |= 1;
break;
}
}
}
return result;
}
private:
int bit_length(int x) {
return (x ? std::__lg(x) : -1) + 1;
}
};
// Time: O(nlogr), r = max(nums)
// Space: O(n)
// bit manipulation, brute force
class Solution4 {
public:
int maximumStrongPairXor(vector<int>& nums) {
int result = 0;
for (int i = 0; i < size(nums); ++i) {
for (int j = i; j < size(nums); ++j) {
if (abs(nums[i] - nums[j]) <= min(nums[i], nums[j])) {
result = max(result, nums[i] ^ nums[j]);
}
}
}
return result;
}
};