Minimum Operations to Convert Number LeetCode solution in Python

1
# Runtime: 4131 ms (Top 24.3%) | Memory: 185.60 MB (Top 35.5%)
2

3

4
class Solution:
5
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
6
ans = 0
7
seen = {start}
8
queue = deque([start])
9
while queue:
10
for _ in range(len(queue)):
11
val = queue.popleft()
12
if val == goal:
13
return ans
14
if 0 <= val <= 1000:
15
for x in nums:
16
for op in (add, sub, xor):
17
if op(val, x) not in seen:
18
seen.add(op(val, x))
19
queue.append(op(val, x))
20
ans += 1
21
return -1

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0