class Solution:
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
def next(x):
res = []
for num in nums:
res.append(x + num)
res.append(x - num)
res.append(x ^ num)
return res
def extend(m1, m2, q):
for _ in range(len(q)):
x = q.popleft()
step = m1[x]
for y in next(x):
if y in m1:
continue
if y in m2:
return step + 1 + m2[y]
if 0 <= y <= 1000:
m1[y] = step + 1
q.append(y)
return -1
m1, m2 = {start: 0}, {goal: 0}
q1, q2 = deque([start]), deque([goal])
while q1 and q2:
t = extend(m1, m2, q1) if len(q1) <= len(q2) else extend(m2, m1, q2)
if t != -1:
return t
return -1