Skip to content

Commit 0aed2d7

Browse files
committed
feat: add solutions to lc problem: No.1707
No.1707.Maximum XOR With an Element From Array
1 parent 629df4f commit 0aed2d7

File tree

6 files changed

+644
-0
lines changed

6 files changed

+644
-0
lines changed

solution/1700-1799/1707.Maximum XOR With an Element From Array/README.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,242 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:前缀树**
48+
49+
50+
4751
<!-- tabs:start -->
4852

4953
### **Python3**
5054

5155
<!-- 这里可写当前语言的特殊实现逻辑 -->
5256

5357
```python
58+
class Trie:
59+
def __init__(self):
60+
self.children = [None] * 2
61+
62+
def insert(self, x):
63+
node = self
64+
for i in range(30, -1, -1):
65+
v = (x >> i) & 1
66+
if node.children[v] is None:
67+
node.children[v] = Trie()
68+
node = node.children[v]
69+
70+
def search(self, x):
71+
node = self
72+
ans = 0
73+
for i in range(30, -1, -1):
74+
v = (x >> i) & 1
75+
if node.children[v ^ 1]:
76+
ans |= 1 << i
77+
node = node.children[v ^ 1]
78+
elif node.children[v]:
79+
node = node.children[v]
80+
else:
81+
return -1
82+
return ans
5483

84+
85+
class Solution:
86+
def maximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]:
87+
trie = Trie()
88+
nums.sort()
89+
j, n = 0, len(queries)
90+
ans = [-1] * n
91+
for i, (x, m) in sorted(zip(range(n), queries), key=lambda x: x[1][1]):
92+
while j < len(nums) and nums[j] <= m:
93+
trie.insert(nums[j])
94+
j += 1
95+
ans[i] = trie.search(x)
96+
return ans
5597
```
5698

5799
### **Java**
58100

59101
<!-- 这里可写当前语言的特殊实现逻辑 -->
60102

61103
```java
104+
class Solution {
105+
public int[] maximizeXor(int[] nums, int[][] queries) {
106+
Trie trie = new Trie();
107+
Arrays.sort(nums);
108+
int n = queries.length;
109+
int[] ans = new int[n];
110+
int[][] qs = new int[n][3];
111+
for (int i = 0; i < n; ++i) {
112+
qs[i] = new int[] {i, queries[i][0], queries[i][1]};
113+
}
114+
Arrays.sort(qs, (a, b) -> a[2] - b[2]);
115+
int j = 0;
116+
for (var q : qs) {
117+
int i = q[0], x = q[1], m = q[2];
118+
while (j < nums.length && nums[j] <= m) {
119+
trie.insert(nums[j++]);
120+
}
121+
ans[i] = trie.search(x);
122+
}
123+
return ans;
124+
}
125+
}
126+
127+
class Trie {
128+
Trie[] children = new Trie[2];
129+
130+
public void insert(int x) {
131+
Trie node = this;
132+
for (int i = 30; i >= 0; --i) {
133+
int v = (x >> i) & 1;
134+
if (node.children[v] == null) {
135+
node.children[v] = new Trie();
136+
}
137+
node = node.children[v];
138+
}
139+
}
140+
141+
public int search(int x) {
142+
Trie node = this;
143+
int ans = 0;
144+
for (int i = 30; i >= 0; --i) {
145+
int v = (x >> i) & 1;
146+
if (node.children[v ^ 1] != null) {
147+
ans |= 1 << i;
148+
node = node.children[v ^ 1];
149+
} else if (node.children[v] != null) {
150+
node = node.children[v];
151+
} else {
152+
return -1;
153+
}
154+
}
155+
return ans;
156+
}
157+
}
158+
```
159+
160+
### **C++**
161+
162+
```cpp
163+
class Trie {
164+
public:
165+
Trie()
166+
: children(2) {}
167+
void insert(int x) {
168+
Trie* node = this;
169+
for (int i = 30; ~i; --i) {
170+
int v = (x >> i) & 1;
171+
if (!node->children[v]) {
172+
node->children[v] = new Trie();
173+
}
174+
node = node->children[v];
175+
}
176+
}
177+
178+
int search(int x) {
179+
int ans = 0;
180+
Trie* node = this;
181+
for (int i = 30; ~i; --i) {
182+
int v = (x >> i) & 1;
183+
if (node->children[v ^ 1]) {
184+
node = node->children[v ^ 1];
185+
ans |= 1 << i;
186+
} else if (node->children[v]) {
187+
node = node->children[v];
188+
} else {
189+
return -1;
190+
}
191+
}
192+
return ans;
193+
}
194+
195+
private:
196+
vector<Trie*> children;
197+
};
198+
199+
class Solution {
200+
public:
201+
vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) {
202+
sort(nums.begin(), nums.end());
203+
int n = queries.size();
204+
vector<tuple<int, int, int>> qs;
205+
for (int i = 0; i < n; ++i) {
206+
qs.push_back({queries[i][1], queries[i][0], i});
207+
}
208+
sort(qs.begin(), qs.end());
209+
Trie* trie = new Trie();
210+
int j = 0;
211+
vector<int> ans(n);
212+
for (auto& [m, x, i] : qs) {
213+
while (j < nums.size() && nums[j] <= m) {
214+
trie->insert(nums[j++]);
215+
}
216+
ans[i] = trie->search(x);
217+
}
218+
return ans;
219+
}
220+
};
221+
```
222+
223+
### **Go**
224+
225+
```go
226+
type Trie struct {
227+
children [2]*Trie
228+
}
229+
230+
func newTrie() *Trie {
231+
return &Trie{}
232+
}
233+
func (this *Trie) insert(x int) {
234+
node := this
235+
for i := 30; i >= 0; i-- {
236+
v := (x >> i) & 1
237+
if node.children[v] == nil {
238+
node.children[v] = newTrie()
239+
}
240+
node = node.children[v]
241+
}
242+
}
243+
244+
func (this *Trie) search(x int) int {
245+
node := this
246+
ans := 0
247+
for i := 30; i >= 0; i-- {
248+
v := (x >> i) & 1
249+
if node.children[v^1] != nil {
250+
node = node.children[v^1]
251+
ans |= 1 << i
252+
} else if node.children[v] != nil {
253+
node = node.children[v]
254+
} else {
255+
return -1
256+
}
257+
}
258+
return ans
259+
}
62260
261+
func maximizeXor(nums []int, queries [][]int) []int {
262+
sort.Ints(nums)
263+
type tuple struct{ i, x, m int }
264+
n := len(queries)
265+
qs := make([]tuple, n)
266+
for i, q := range queries {
267+
qs[i] = tuple{i, q[0], q[1]}
268+
}
269+
sort.Slice(qs, func(i, j int) bool { return qs[i].m < qs[j].m })
270+
j := 0
271+
ans := make([]int, n)
272+
trie := newTrie()
273+
for _, q := range qs {
274+
i, x, m := q.i, q.x, q.m
275+
for j < len(nums) && nums[j] <= m {
276+
trie.insert(nums[j])
277+
j++
278+
}
279+
ans[i] = trie.search(x)
280+
}
281+
return ans
282+
}
63283
```
64284

65285
### **...**

0 commit comments

Comments
 (0)