forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03. Maximum XOR With an Element From Array.py
64 lines (53 loc) Β· 1.85 KB
/
03. Maximum XOR With an Element From Array.py
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
# https://leetcode.com/problems/maximum-xor-with-an-element-from-array/
# First SOLVE: 03. Maximum XOR of Two Numbers in an Array (https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)
class TrieNode:
def __init__(self):
self.children = {}
self.val = 0
class Solution:
def __init__(self):
self.root = TrieNode()
def addNum(self, num):
cur = self.root
for i in range(31, -1, -1):
bit = 1 if num & (1 << i) else 0
if bit not in cur.children:
cur.children[bit] = TrieNode()
cur = cur.children[bit]
cur.val = num
def getMax(self, x):
cur = self.root
maxi = 0
for i in range(31, -1, -1):
bit = 1 if x & (1 << i) else 0
if bit == 1:
if 0 in cur.children:
cur = cur.children[0]
elif 1 not in cur.children:
return -1
else:
cur = cur.children[1]
else: # bit = 0
if 1 in cur.children:
cur = cur.children[1]
elif 0 not in cur.children:
return -1
else:
cur = cur.children[0]
maxi = cur.val ^ x
# print(maxi)
return maxi
def maximizeXor(self, nums, queries):
nums.sort()
res = [-1] * (len(queries))
for i in range(len(queries)):
queries[i].append(i)
queries.sort(key = lambda x:x[1])
n = 0; q = 0
while q < len(queries):
while n < len(nums) and nums[n] <= queries[q][1]:
self.addNum(nums[n])
n += 1
res[queries[q][2]] = self.getMax(queries[q][0])
q += 1
return res