-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path421.cpp
More file actions
54 lines (51 loc) · 1.14 KB
/
421.cpp
File metadata and controls
54 lines (51 loc) · 1.14 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
class TrieNode {
public:
TrieNode* child[2];
TrieNode(){
this->child[0] = nullptr;
this->child[1] = nullptr;
}
};
class Trie {
private:
TrieNode* root;
public:
Trie(){
root = new TrieNode();
}
void insert(int x){
TrieNode* node = root;
bitset<32> bs(x);
for (int i = 31; i >= 0; i--){
if (node->child[bs[i]] == nullptr) node->child[bs[i]] = new TrieNode();
node = node->child[bs[i]];
}
}
int findMax(int x){
TrieNode* node = root;
bitset<32> bs(x);
int ans = 0;
for (int i = 31; i >= 0; i--){
if (node->child[!bs[i]] != nullptr) {
ans += 1 << i;
node = node->child[!bs[i]];
}
else node = node->child[bs[i]];
}
return ans;
}
};
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
Trie t = Trie();
for (auto num : nums){
t.insert(num);
}
int ans = 0;
for (auto num : nums){
ans = max(ans, t.findMax(num));
}
return ans;
}
};