-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmaximum-xor-of-two-numbers-in-an-array.py
64 lines (41 loc) · 1.39 KB
/
maximum-xor-of-two-numbers-in-an-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
from typing import List, Dict
class IntTrieNode:
def __init__(self) -> None:
self.children: Dict[int, IntTrieNode] = {}
def trie_add(root: IntTrieNode, num: int) -> None:
node = root
for shift in reversed(range(32)):
cur_bit = (num >> shift) & 1
node.children.setdefault(cur_bit, IntTrieNode())
node = node.children[cur_bit]
def trie_largest_xor(root: IntTrieNode, num: int) -> int:
node = root
result = 0
for shift in reversed(range(32)):
inv_bit = ~(num >> shift) & 1
cur_bit = (num >> shift) & 1
result <<= 1
if inv_bit in node.children:
result |= 1
node = node.children[inv_bit]
else:
node = node.children[cur_bit]
return result
class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
root = IntTrieNode()
for num in nums:
trie_add(root, num)
result = 0
for num in nums:
result = max(result, trie_largest_xor(root, num))
return result
def findMaximumXOR1(self, nums: List[int]) -> int:
result = 0
for mask_len in reversed(range(32)):
prefixes = {num >> mask_len for num in nums}
result <<= 1
for prefix in prefixes:
if (result | 1) ^ prefix in prefixes:
result |= 1
return result