Skip to content

Commit f229d93

Browse files
authored
Create 1514-path-with-maximum-probability.py
1 parent a91888e commit f229d93

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: python/1514-path-with-maximum-probability.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
3+
adj = collections.defaultdict(list)
4+
for i in range(len(edges)):
5+
src, dst = edges[i]
6+
adj[src].append([dst, succProb[i]])
7+
adj[dst].append([src, succProb[i]])
8+
9+
pq = [(-1, start)]
10+
visit = set()
11+
12+
while pq:
13+
prob, cur = heapq.heappop(pq)
14+
visit.add(cur)
15+
16+
if cur == end:
17+
return prob * -1
18+
for nei, edgeProb in adj[cur]:
19+
if nei not in visit:
20+
heapq.heappush(pq, (prob * edgeProb, nei))
21+
return 0

0 commit comments

Comments
 (0)