Skip to content

Commit 9dd58ac

Browse files
add 1514
1 parent acaee0f commit 9dd58ac

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1514|[Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1514.java) | |Medium|Graph|
1112
|1512|[Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1512.java) | |Easy|Array, HashTable, Math|
1213
|1508|[Range Sum of Sorted Subarray Sums](https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1508.java) | |Medium|Array, Sort|
1314
|1507|[Reformat Date](https://leetcode.com/problems/reformat-date/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1507.java) | |Easy|String|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class _1514 {
9+
public static class Solution1 {
10+
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
11+
List<Integer>[] nodeToNodesList = new List[n];
12+
List<Double>[] nodeToProbabilitiesList = new List[n];
13+
for (int i = 0; i < n; i++) {
14+
nodeToNodesList[i] = new ArrayList<>();
15+
nodeToProbabilitiesList[i] = new ArrayList<>();
16+
}
17+
for (int i = 0; i < edges.length; i++) {
18+
int u = edges[i][0];
19+
int v = edges[i][1];
20+
double w = succProb[i];
21+
nodeToNodesList[u].add(v);
22+
nodeToProbabilitiesList[u].add(w);
23+
24+
nodeToNodesList[v].add(u);
25+
nodeToProbabilitiesList[v].add(w);
26+
}
27+
28+
double[] probabilities = new double[n];
29+
probabilities[start] = 1.0;
30+
boolean[] visited = new boolean[n];
31+
Queue<Integer> queue = new ArrayDeque<>();
32+
queue.add(start);
33+
visited[start] = true;
34+
while (!queue.isEmpty()) {
35+
int u = queue.poll();
36+
visited[u] = false;
37+
38+
for (int i = 0; i < nodeToNodesList[u].size(); i++) {
39+
int v = nodeToNodesList[u].get(i);
40+
double w = nodeToProbabilitiesList[u].get(i);
41+
if (probabilities[u] * w > probabilities[v]) {
42+
probabilities[v] = probabilities[u] * w;
43+
if (!visited[v]) {
44+
visited[v] = true;
45+
queue.add(v);
46+
}
47+
}
48+
}
49+
}
50+
51+
return probabilities[end];
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy