Skip to content

Commit ce9582d

Browse files
authored
Update maximum-strong-pair-xor-i.py
1 parent 0fbf394 commit ce9582d

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

Python/maximum-strong-pair-xor-i.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,19 @@ def insert(self, num):
8787
self.__maxs[curr] = max(self.__maxs[curr], num)
8888

8989
def query(self, num):
90-
result = curr1 = curr2 = 0
90+
result = curr = 0
9191
for i in reversed(xrange(self.__bit_length)):
9292
result <<= 1
9393
x = num>>i
9494
y = (result|1)^x
95-
assert(x != y)
96-
a, b = self.__nodes[curr1][x&1], self.__nodes[curr2][y&1]
97-
if x < y:
98-
a, b = b, a
99-
if self.__nodes[curr2][y&1] != -1 and self.__mins[a] <= 2*self.__maxs[b]:
95+
assert(x != y)
96+
if (self.__nodes[curr][y&1] != -1 and
97+
((x > y and num <= 2*self.__maxs[self.__nodes[curr][y&1]]) or
98+
(x < y and self.__mins[self.__nodes[curr][y&1]] <= 2*num))):
10099
result |= 1
101-
curr2 = self.__nodes[curr2][y&1]
102-
elif self.__nodes[curr2][(y&1)^1] != -1:
103-
curr2 = self.__nodes[curr2][(y&1)^1]
100+
curr = self.__nodes[curr][y&1]
104101
else:
105-
break
106-
curr1 = self.__nodes[curr1][x&1]
102+
curr = self.__nodes[curr][1^(y&1)]
107103
return result
108104

109105
trie = Trie(max(nums).bit_length())

0 commit comments

Comments
 (0)