-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0847-shortest-path-visiting-all-nodes.py
More file actions
30 lines (25 loc) · 912 Bytes
/
0847-shortest-path-visiting-all-nodes.py
File metadata and controls
30 lines (25 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from typing import List
class Solution:
def shortestPathLength(self, graph: List[List[int]]) -> int:
if len(graph) == 1:
return 0
n = len(graph)
endingMask = (1<<n) - 1
queue = [(node, 1<< node) for node in range(n)]
seen = set(queue)
steps = 0
while queue:
nextQueue = []
for i in range(len(queue)):
node, mask = queue[i]
for neighbor in graph[node]:
nextMask = mask | (1 << neighbor)
if nextMask == endingMask:
return 1 + steps
if (neighbor , nextMask) not in seen:
seen.add((neighbor, nextMask))
nextQueue.append((neighbor, nextMask))
steps += 1
queue = nextQueue
return steps
graph = [[1, 2, 3], [0], [0], [0]]