forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe-most-similar-path-in-a-graph.py
More file actions
29 lines (26 loc) · 987 Bytes
/
the-most-similar-path-in-a-graph.py
File metadata and controls
29 lines (26 loc) · 987 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
# Time: O(n^2 * m), m is the length of targetPath
# Space: O(n * m)
class Solution(object):
def mostSimilar(self, n, roads, names, targetPath):
"""
:type n: int
:type roads: List[List[int]]
:type names: List[str]
:type targetPath: List[str]
:rtype: List[int]
"""
adj = [[] for _ in xrange(n)]
for u, v in roads:
adj[u].append(v)
adj[v].append(u)
dp = [[0]*n for _ in xrange(len(targetPath)+1)]
for i in xrange(1, len(targetPath)+1):
for v in xrange(n):
dp[i][v] = (names[v] != targetPath[i-1]) + min(dp[i-1][u] for u in adj[v])
path = [dp[-1].index(min(dp[-1]))]
for i in reversed(xrange(2, len(targetPath)+1)):
for u in adj[path[-1]]:
if dp[i-1][u]+(names[path[-1]] != targetPath[i-1]) == dp[i][path[-1]]:
path.append(u)
break
return path[::-1]