Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c71dbe

Browse files
Add solution for Path with Maximum Probability
1 parent 667b164 commit 8c71dbe

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
9595
- Shortest Path to Get Food | [Problem](https://leetcode.com/problems/shortest-path-to-get-food) | [Java Solution](src/javacode/solutions/ShortestPathToGetFood.java)
9696
- Shortest Distance from All Buildings | [Problem](https://leetcode.com/problems/shortest-distance-from-all-buildings) | [Java Solution](src/javacode/solutions/ShortestDistanceFromAllBuildings.java)
9797
- Find if Path Exists in Graph | [Problem](https://leetcode.com/problems/find-if-path-exists-in-graph) | [Java Solution](src/javacode/solutions/FindIfPathExistsInGraph.java)
98+
- Path with Maximum Probability | [Problem](https://leetcode.com/problems/path-with-maximum-probability) | [Java Solution](src/javacode/solutions/PathWithMaximumProbability.java)
9899

99100
### DFS
100101
- Path Sum | [Problem](https://leetcode.com/problems/path-sum) | [JS Solution](src/javascript/solutions/pathSum.js)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package javacode.solutions;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.HashSet;
5+
import java.util.Queue;
6+
import java.util.Set;
7+
8+
// [Problem] https://leetcode.com/problems/path-with-maximum-probability
9+
class PathWithMaximumProbability {
10+
// BFS
11+
// O(v + e) time, O(v + e) space
12+
public double maxProbability(int n, int[][] edges, double[] probabilities, int start, int end) {
13+
HashSet<Edge>[] graph = new HashSet[n];
14+
for (int i = 0; i < n; i++) {
15+
graph[i] = new HashSet<>();
16+
}
17+
for (int j = 0; j < edges.length; j++) {
18+
int[] edge = edges[j];
19+
double probability = probabilities[j];
20+
graph[edge[0]].add(new Edge(edge[1], probability));
21+
graph[edge[1]].add(new Edge(edge[0], probability));
22+
}
23+
Queue<Edge> edgesToVisit = new ArrayDeque<>();
24+
double[] maxProbabilities = new double[n];
25+
edgesToVisit.add(new Edge(start, 1D));
26+
while (!edgesToVisit.isEmpty()) {
27+
Edge edge = edgesToVisit.poll();
28+
int sourceNode = edge.node;
29+
double probability = edge.probability;
30+
Set<Edge> nextEdges = graph[sourceNode];
31+
for (Edge nextEdge : nextEdges) {
32+
int nextNode = nextEdge.node;
33+
double newProbability = probability * nextEdge.probability;
34+
if (maxProbabilities[nextNode] < newProbability) {
35+
edgesToVisit.add(new Edge(nextNode, newProbability));
36+
maxProbabilities[nextNode] = newProbability;
37+
}
38+
}
39+
}
40+
return maxProbabilities[end];
41+
}
42+
43+
// Test
44+
public static void main(String[] args) {
45+
PathWithMaximumProbability solution = new PathWithMaximumProbability();
46+
47+
int[][] edges = {{0, 1}, {1, 2}, {0, 2}};
48+
double[] probabilities = {0.5, 0.5, 0.2};
49+
int n = 3, start = 0, end = 2;
50+
double expectedOutput = 0.25;
51+
double actualOutput = solution.maxProbability(n, edges, probabilities, start, end);
52+
53+
System.out.println("Test passed? " + (expectedOutput == actualOutput));
54+
}
55+
}
56+
57+
class Edge {
58+
int node;
59+
double probability;
60+
61+
public Edge(int node, double probability) {
62+
this.node = node;
63+
this.probability = probability;
64+
}
65+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /