MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / maxProbability

Method maxProbability

python/1514-path-with-maximum-probability.py:2–21  ·  view source on GitHub ↗
(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int)

Source from the content-addressed store, hash-verified

1class 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

Callers

nothing calls this directly

Calls 1

addMethod · 0.45

Tested by

no test coverage detected