-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_1514.java
28 lines (26 loc) · 936 Bytes
/
Leetcode_1514.java
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
class Solution {
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
double[] maxProb = new double[n];
maxProb[start] = 1.0;
for (int i = 0; i < n - 1; i++) {
boolean hasUpdate = false;
for (int j = 0; j < edges.length; j++) {
int u = edges[j][0];
int v = edges[j][1];
double pathProb = succProb[j];
if (maxProb[u] * pathProb > maxProb[v]) {
maxProb[v] = maxProb[u] * pathProb;
hasUpdate = true;
}
if (maxProb[v] * pathProb > maxProb[u]) {
maxProb[u] = maxProb[v] * pathProb;
hasUpdate = true;
}
}
if (!hasUpdate) {
break;
}
}
return maxProb[end];
}
}