Approach
Breadth-first search
For Path with Maximum Probability, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 41 lines of Java from the credited upstream file 1514.java.
- The implementation visibly relies on sequence storage, work queue.
- 3 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {3 4 List<Pair<Integer, Double>>[] graph = new List[n];5 6 Queue<Pair<Double, Integer>> maxHeap =7 new PriorityQueue<>((a, b) -> Double.compare(b.getKey(), a.getKey())) {8 { offer(new Pair<>(1.0, start)); }9 };10 boolean[] seen = new boolean[n];11 Arrays.setAll(graph, i -> new ArrayList<>());12 13 for (int i = 0; i < edges.length; ++i) {14 final int u = edges[i][0];15 final int v = edges[i][1];16 final double prob = succProb[i];17 graph[u].add(new Pair<>(v, prob));18 graph[v].add(new Pair<>(u, prob));19 }20 21 while (!maxHeap.isEmpty()) {22 final double prob = maxHeap.peek().getKey();23 final int u = maxHeap.poll().getValue();24 if (u == end)25 return prob;26 if (seen[u])27 continue;28 seen[u] = true;29 for (Pair<Integer, Double> node : graph[u]) {30 final int nextNode = node.getKey();31 final double edgeProb = node.getValue();32 if (seen[nextNode])33 continue;34 maxHeap.add(new Pair<>(prob * edgeProb, nextNode));35 }36 }37 38 return 0;39 }40}41