voidinsert(int val){ Trie* node = this; for (int i = L - 1; i >= 0; --i) { int bit = (val >> i) & 1; if (node->children[bit] == nullptr) { node->children[bit] = newTrie(); } node = node->children[bit]; } }
intgetMaxXor(int val){ int ans = 0; Trie* node = this; for (int i = L - 1; i >= 0; --i) { int bit = (val >> i) & 1; if (node->children[bit ^ 1] != nullptr) { ans |= 1 << i; bit ^= 1; } node = node->children[bit]; } return ans; } };
classSolution { public: vector<int> maximizeXor(vector<int> &nums, vector<vector<int>> &queries){ sort(nums.begin(), nums.end()); int numQ = queries.size(); for (int i = 0; i < numQ; ++i) { queries[i].push_back(i); } sort(queries.begin(), queries.end(), [](auto &x, auto &y) { return x[1] < y[1]; });
vector<int> ans(numQ); Trie* t = newTrie(); int idx = 0, n = nums.size(); for (auto &q : queries) { int x = q[0], m = q[1], qid = q[2]; while (idx < n && nums[idx] <= m) { t->insert(nums[idx]); ++idx; } if (idx == 0) { // 字典树为空 ans[qid] = -1; } else { ans[qid] = t->getMaxXor(x); } } return ans; } };
publicclassSolution { publicint[] MaximizeXor(int[] nums, int[][] queries) { Array.Sort(nums); int numQ = queries.Length; Tuple<int, int, int>[] newQueries = new Tuple<int, int, int>[numQ]; for (int i = 0; i < numQ; ++i) { newQueries[i] = new Tuple<int, int, int>(queries[i][0], queries[i][1], i); } Array.Sort<Tuple<int, int, int>>(newQueries, delegate(Tuple<int, int, int> query1, Tuple<int, int, int> query2) { return query1.Item2 - query2.Item2; } );
int[] ans = newint[numQ]; Trie trie = new Trie(); int idx = 0, n = nums.Length; foreach (Tuple<int, int, int> query in newQueries) { int x = query.Item1, m = query.Item2, qid = query.Item3; while (idx < n && nums[idx] <= m) { trie.Insert(nums[idx]); ++idx; } if (idx == 0) { // 字典树为空 ans[qid] = -1; } else { ans[qid] = trie.GetMaxXor(x); } } return ans; } }
classTrie { constint L = 30; Trie[] children = new Trie[2];
publicvoidInsert(int val) { Trie node = this; for (int i = L - 1; i >= 0; --i) { int bit = (val >> i) & 1; if (node.children[bit] == null) { node.children[bit] = new Trie(); } node = node.children[bit]; } }
publicintGetMaxXor(int val) { int ans = 0; Trie node = this; for (int i = L - 1; i >= 0; --i) { int bit = (val >> i) & 1; if (node.children[bit ^ 1] != null) { ans |= 1 << i; bit ^= 1; } node = node.children[bit]; } return ans; } }
func(t *trie) insert(val int) { node := t for i := L - 1; i >= 0; i-- { bit := val >> i & 1 if node.children[bit] == nil { node.children[bit] = &trie{} } node = node.children[bit] } }
func(t *trie) getMaxXor(val int) (ans int) { node := t for i := L - 1; i >= 0; i-- { bit := val >> i & 1 if node.children[bit^1] != nil { ans |= 1 << i bit ^= 1 } node = node.children[bit] } return }
funcmaximizeXor(nums []int, queries [][]int) []int { sort.Ints(nums) for i := range queries { queries[i] = append(queries[i], i) } sort.Slice(queries, func(i, j int)bool { return queries[i][1] < queries[j][1] })
ans := make([]int, len(queries)) t := &trie{} idx, n := 0, len(nums) for _, q := range queries { x, m, qid := q[0], q[1], q[2] for idx < n && nums[idx] <= m { t.insert(nums[idx]) idx++ } if idx == 0 { // 字典树为空 ans[qid] = -1 } else { ans[qid] = t.getMaxXor(x) } } return ans }
definsert(self, val: int): node = self for i inrange(Trie.L, -1, -1): bit = (val >> i) & 1 if bit == 0: ifnot node.left: node.left = Trie() node = node.left else: ifnot node.right: node.right = Trie() node = node.right defgetMaxXor(self, val: int) -> int: ans, node = 0, self for i inrange(Trie.L, -1, -1): bit = (val >> i) & 1 check = False if bit == 0: if node.right: node = node.right check = True else: node = node.left else: if node.left: node = node.left check = True else: node = node.right if check: ans |= 1 << i return ans
classSolution: defmaximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]: n, q = len(nums), len(queries) nums.sort() queries = sorted([(x, m, i) for i, (x, m) inenumerate(queries)], key=lambda query: query[1]) ans = [0] * q t = Trie() idx = 0 for x, m, qid in queries: while idx < n and nums[idx] <= m: t.insert(nums[idx]) idx += 1 if idx == 0: # 字典树为空 ans[qid] = -1 else: ans[qid] = t.getMaxXor(x) return ans
voidinsert(struct TrieNode* root, int val) { structTrieNode* node = root; for (int i = L - 1; i >= 0; --i) { int bit = (val >> i) & 1; if (node->children[bit] == NULL) { node->children[bit] = createTrieNode(); } node = node->children[bit]; } }
intgetMaxXor(struct TrieNode* root, int val) { int ans = 0; structTrieNode* node = root; for (int i = L - 1; i >= 0; --i) { int bit = (val >> i) & 1; if (node->children[bit ^ 1] != NULL) { ans |= 1 << i; bit ^= 1; } node = node->children[bit]; } return ans; }
intcmp1(int* a, int* b) { return *a - *b; }
intcmp2(int** a, int** b) { return (*a)[1] - (*b)[1]; }
int* maximizeXor(int* nums, int numsSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize) { qsort(nums, numsSize, sizeof(int), cmp1); for (int i = 0; i < queriesSize; ++i) { queries[i] = realloc(queries[i], sizeof(int) * 3); queries[i][2] = i; } qsort(queries, queriesSize, sizeof(int*), cmp2); int* ans = malloc(sizeof(int) * queriesSize); *returnSize = queriesSize; structTrieNode* t = createTrieNode(); int idx = 0, n = numsSize; for (int i = 0; i < queriesSize; i++) { int x = queries[i][0], m = queries[i][1], qid = queries[i][2]; while (idx < n && nums[idx] <= m) { insert(t, nums[idx]); ++idx; } if (idx == 0) { // 字典树为空 ans[qid] = -1; } else { ans[qid] = getMaxXor(t, x); } } return ans; }
func(t *trie) insert(val int) { node := t if val < node.min { node.min = val } for i := L - 1; i >= 0; i-- { bit := val >> i & 1 if node.children[bit] == nil { node.children[bit] = &trie{min: val} } node = node.children[bit] if val < node.min { node.min = val } } }
func(t *trie) getMaxXorWithLimit(val, limit int) (ans int) { node := t if node.min > limit { return-1 } for i := L - 1; i >= 0; i-- { bit := val >> i & 1 if node.children[bit^1] != nil && node.children[bit^1].min <= limit { ans |= 1 << i bit ^= 1 } node = node.children[bit] } return }
funcmaximizeXor(nums []int, queries [][]int) []int { t := &trie{min: math.MaxInt32} for _, val := range nums { t.insert(val) } ans := make([]int, len(queries)) for i, q := range queries { ans[i] = t.getMaxXorWithLimit(q[0], q[1]) } return ans }
definsert(self, val: int): node = self node.min_value = min(node.min_value, val) for i inrange(Trie.L, -1, -1): bit = (val >> i) & 1 if bit == 0: ifnot node.left: node.left = Trie() node = node.left else: ifnot node.right: node.right = Trie() node = node.right node.min_value = min(node.min_value, val) defgetMaxXorWithLimit(self, val: int, limit: int) -> int: node = self if node.min_value > limit: return -1 ans = 0 for i inrange(Trie.L, -1, -1): bit = (val >> i) & 1 check = False if bit == 0: if node.right and node.right.min_value <= limit: node = node.right check = True else: node = node.left else: if node.left and node.left.min_value <= limit: node = node.left check = True else: node = node.right if check: ans |= 1 << i return ans
classSolution: defmaximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]: t = Trie() for val in nums: t.insert(val) q = len(queries) ans = [0] * q for i, (x, m) inenumerate(queries): ans[i] = t.getMaxXorWithLimit(x, m) return ans