Skip to content

Commit ef73a6d

Browse files
Sync LeetCode submission Runtime - 56 ms (39.85%), Memory - 74.6 MB (21.30%)
1 parent b36f85f commit ef73a6d

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>You are given an undirected weighted graph of&nbsp;<code>n</code>&nbsp;nodes (0-indexed), represented by an edge list where&nbsp;<code>edges[i] = [a, b]</code>&nbsp;is an undirected edge connecting the nodes&nbsp;<code>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;with a probability of success of traversing that edge&nbsp;<code>succProb[i]</code>.</p>
2+
3+
<p>Given two nodes&nbsp;<code>start</code>&nbsp;and&nbsp;<code>end</code>, find the path with the maximum probability of success to go from&nbsp;<code>start</code>&nbsp;to&nbsp;<code>end</code>&nbsp;and return its success probability.</p>
4+
5+
<p>If there is no path from&nbsp;<code>start</code>&nbsp;to&nbsp;<code>end</code>, <strong>return&nbsp;0</strong>. Your answer will be accepted if it differs from the correct answer by at most <strong>1e-5</strong>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex1.png" style="width: 187px; height: 186px;" /></strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
14+
<strong>Output:</strong> 0.25000
15+
<strong>Explanation:</strong>&nbsp;There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex2.png" style="width: 189px; height: 186px;" /></strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
24+
<strong>Output:</strong> 0.30000
25+
</pre>
26+
27+
<p><strong class="example">Example 3:</strong></p>
28+
29+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex3.png" style="width: 215px; height: 191px;" /></strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
33+
<strong>Output:</strong> 0.00000
34+
<strong>Explanation:</strong>&nbsp;There is no path between 0 and 2.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>2 &lt;= n &lt;= 10^4</code></li>
42+
<li><code>0 &lt;= start, end &lt; n</code></li>
43+
<li><code>start != end</code></li>
44+
<li><code>0 &lt;= a, b &lt; n</code></li>
45+
<li><code>a != b</code></li>
46+
<li><code>0 &lt;= succProb.length == edges.length &lt;= 2*10^4</code></li>
47+
<li><code>0 &lt;= succProb[i] &lt;= 1</code></li>
48+
<li>There is at most one edge between every two nodes.</li>
49+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
4+
int m = edges.size();
5+
unordered_map<int, vector<pair<int, double>>> graph;
6+
for (int i=0; i<m; i++) {
7+
graph[edges[i][0]].push_back({edges[i][1], succProb[i]});
8+
graph[edges[i][1]].push_back({edges[i][0], succProb[i]});
9+
}
10+
11+
vector<double> probs(n, 0);
12+
priority_queue<pair<double, int>> pq;
13+
probs[start_node] = 1;
14+
pq.push({1, start_node});
15+
while (!pq.empty()) {
16+
auto [prob, node] = pq.top();
17+
if (node == end_node) return prob;
18+
pq.pop();
19+
20+
if (prob < probs[node]) continue;
21+
22+
for (auto& neigh : graph[node]) {
23+
int newNode = neigh.first;
24+
double newProb = prob * neigh.second;
25+
26+
if (newProb > probs[newNode]) {
27+
probs[newNode] = newProb;
28+
pq.push({newProb, newNode});
29+
}
30+
}
31+
}
32+
33+
return probs[end_node];
34+
}
35+
};

0 commit comments

Comments
 (0)