- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 61 lines of Python from the credited upstream file 2932.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class TrieNode:2 def __init__(self):3 self.children: list[TrieNode | None] = [None] * 24 self.mn = math.inf5 self.mx = -math.inf6 7 8class BitTrie:9 def __init__(self, maxBit: int):10 self.maxBit = maxBit11 self.root = TrieNode()12 13 def insert(self, num: int) -> None:14 node = self.root15 for i in range(self.maxBit, -1, -1):16 bit = num >> i & 117 if not node.children[bit]:18 node.children[bit] = TrieNode()19 node = node.children[bit]20 node.mn = min(node.mn, num)21 node.mx = max(node.mx, num)22 23 def getMaxXor(self, x: int) -> int:24 """Returns max(x ^ y) where |x - y| <= min(x, y).25 26 If x <= y, |x - y| <= min(x, y) can be written as y - x <= x.27 So, y <= 2 * x.28 """29 maxXor = 030 node = self.root31 for i in range(self.maxBit, -1, -1):32 bit = x >> i & 133 toggleBit = bit ^ 134 35 36 37 38 if (node.children[toggleBit] and39 node.children[toggleBit].mx > x and40 node.children[toggleBit].mn <= 2 * x):41 maxXor = maxXor | 1 << i42 node = node.children[toggleBit]43 elif node.children[bit]:44 node = node.children[bit]45 else: 46 return 047 return maxXor48 49 50class Solution:51 52 def maximumStrongPairXor(self, nums: list[int]) -> int:53 maxNum = max(nums)54 maxBit = int(math.log2(maxNum))55 bitTrie = BitTrie(maxBit)56 57 for num in nums:58 bitTrie.insert(num)59 60 return max(bitTrie.getMaxXor(num) for num in nums)61