1707. Maximum XOR With an Element From Array
You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi]. The answer to the ith query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1. Return an integer array answer where answer.length == queries.length and answer[i] is the answer to the ith query.
class Trie:
def __init__(self):
self.root = {}
def insert(self, num):
p = self.root
for i in range(31, -1, -1):
cur = (num >> i) & 1
if cur not in p:
p[cur] = {}
p = p[cur]
def query(self, num):
if not self.root:
return -1
p, ans = self.root, 0
for i in range(31, -1, -1):
cur = (num >> i) & 1
if 1 - cur in p:
p = p[1 - cur]
ans |= (1 << i)
else:
p = p[cur]
return ans
class Solution:
def maximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]:
nums.sort()
queries = sorted(enumerate(queries), key=lambda x: x[1][1])
trie = Trie()
ans = [-1] * len(queries)
j = 0
for i, (x, m) in queries:
while j < len(nums) and nums[j] <= m:
trie.insert(nums[j])
j += 1
ans[i] = trie.query(x)
return ans