Approach
Breadth-first search
For Maximum Subarray Xor with Bounded Range, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 200 lines of Python from the credited upstream file maximum-subarray-xor-with-bounded-range.py.
- The implementation visibly relies on sequence storage, hash lookup, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 4import collections5 6 78class Solution(object):9 def maxXor(self, nums, k):10 """11 :type nums: List[int]12 :type k: int13 :rtype: int14 """15 lookup = [-1]*len(nums)16 max_dq = collections.deque()17 min_dq = collections.deque()18 left = 019 for right in xrange(len(nums)):20 while max_dq and nums[max_dq[-1]] <= nums[right]:21 max_dq.pop()22 max_dq.append(right)23 while min_dq and nums[min_dq[-1]] >= nums[right]:24 min_dq.pop()25 min_dq.append(right)26 while nums[max_dq[0]]-nums[min_dq[0]] > k:27 if max_dq and max_dq[0] == left:28 max_dq.popleft()29 if min_dq and min_dq[0] == left:30 min_dq.popleft()31 left += 132 lookup[right] = left33 result = 034 mx = max(max(nums), 1)35 for i in reversed(xrange(mx.bit_length())):36 lookup2 = collections.defaultdict(int)37 lookup2[0] = prefix = 038 for right in xrange(len(nums)):39 prefix ^= nums[right]>>i40 if ((result>>i)|1)^prefix in lookup2 and lookup2[((result>>i)|1)^prefix] >= lookup[right]:41 result |= 1<<i42 break43 lookup2[prefix] = right+144 return result45 46 474849import collections50 51 5253class Solution2(object):54 def maxXor(self, nums, k):55 """56 :type nums: List[int]57 :type k: int58 :rtype: int59 """60 class Trie(object):61 def __init__(self, bit_length):62 self.__lefts = [-1]*(1+(1+len(nums))*bit_length) 63 self.__rights = [-1]*(1+(1+len(nums))*bit_length)64 self.__cnts = [0]*(1+(1+len(nums))*bit_length)65 self.__i = 066 self.__new_node()67 self.__bit_length = bit_length68 69 def __new_node(self):70 self.__i += 171 return self.__i-172 73 def add(self, num, diff):74 curr = 075 for i in reversed(xrange(self.__bit_length)):76 x = (num>>i)&177 if x == 0:78 if self.__lefts[curr] == -1:79 self.__lefts[curr] = self.__new_node()80 curr = self.__lefts[curr]81 else:82 if self.__rights[curr] == -1:83 self.__rights[curr] = self.__new_node()84 curr = self.__rights[curr]85 self.__cnts[curr] += diff86 87 def query(self, prefix):88 result = curr = 089 for i in reversed(xrange(self.__bit_length)):90 x = (prefix>>i)&191 l, r = (self.__lefts, self.__rights) if x^1 else (self.__rights, self.__lefts)92 if r[curr] != -1 and self.__cnts[r[curr]]:93 result |= 1<<i94 curr = r[curr]95 else:96 curr = l[curr]97 return result98 99 result = 0100 prefix = [0]*(len(nums)+1)101 for i in xrange(len(nums)):102 prefix[i+1] = prefix[i]^nums[i]103 mx = max(max(nums), 1)104 trie = Trie(mx.bit_length())105 trie.add(prefix[0], +1)106 max_dq = collections.deque()107 min_dq = collections.deque()108 left = 0109 for right in xrange(len(nums)):110 while max_dq and nums[max_dq[-1]] <= nums[right]:111 max_dq.pop()112 max_dq.append(right)113 while min_dq and nums[min_dq[-1]] >= nums[right]:114 min_dq.pop()115 min_dq.append(right)116 while nums[max_dq[0]]-nums[min_dq[0]] > k:117 trie.add(prefix[left], -1)118 if max_dq and max_dq[0] == left:119 max_dq.popleft()120 if min_dq and min_dq[0] == left:121 min_dq.popleft()122 left += 1123 result = max(result, trie.query(prefix[right+1]))124 trie.add(prefix[right+1], +1)125 return result126 127 128129130import collections131 132 133134class Solution3(object):135 def maxXor(self, nums, k):136 """137 :type nums: List[int]138 :type k: int139 :rtype: int140 """141 class Trie(object):142 def __init__(self, bit_length):143 self.__nodes = []144 self.__cnts = []145 self.__new_node()146 self.__bit_length = bit_length147 148 def __new_node(self):149 self.__nodes.append([-1]*2)150 self.__cnts.append(0)151 return len(self.__nodes)-1152 153 def add(self, num, diff):154 curr = 0155 for i in reversed(xrange(self.__bit_length)):156 x = (num>>i)&1157 if self.__nodes[curr][x] == -1:158 self.__nodes[curr][x] = self.__new_node()159 curr = self.__nodes[curr][x]160 self.__cnts[curr] += diff161 162 def query(self, prefix):163 result = curr = 0164 for i in reversed(xrange(self.__bit_length)):165 x = (prefix>>i)&1166 if self.__nodes[curr][x^1] != -1 and self.__cnts[self.__nodes[curr][x^1]]:167 result |= 1<<i168 curr = self.__nodes[curr][x^1]169 else:170 curr = self.__nodes[curr][x]171 return result172 173 result = 0174 prefix = [0]*(len(nums)+1)175 for i in xrange(len(nums)):176 prefix[i+1] = prefix[i]^nums[i]177 mx = max(max(nums), 1)178 trie = Trie(mx.bit_length())179 trie.add(prefix[0], +1)180 max_dq = collections.deque()181 min_dq = collections.deque()182 left = 0183 for right in xrange(len(nums)):184 while max_dq and nums[max_dq[-1]] <= nums[right]:185 max_dq.pop()186 max_dq.append(right)187 while min_dq and nums[min_dq[-1]] >= nums[right]:188 min_dq.pop()189 min_dq.append(right)190 while nums[max_dq[0]]-nums[min_dq[0]] > k:191 trie.add(prefix[left], -1)192 if max_dq and max_dq[0] == left:193 max_dq.popleft()194 if min_dq and min_dq[0] == left:195 min_dq.popleft()196 left += 1197 result = max(result, trie.query(prefix[right+1]))198 trie.add(prefix[right+1], +1)199 return result200