Skip to content

Commit 0313b39

Browse files
authored
feat: add solutions to lc problem: No.1514 (#3816)
No.1514.Path with Maximum Probability
1 parent f963418 commit 0313b39

File tree

16 files changed

+405
-591
lines changed

16 files changed

+405
-591
lines changed

solution/1500-1599/1514.Path with Maximum Probability/README.md

+133-201
Large diffs are not rendered by default.

solution/1500-1599/1514.Path with Maximum Probability/README_EN.md

+135-199
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
class Solution {
22
public:
3-
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
4-
vector<vector<pair<int, double>>> g(n);
3+
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
4+
using pdi = pair<double, int>;
5+
vector<pdi> g[n];
56
for (int i = 0; i < edges.size(); ++i) {
67
int a = edges[i][0], b = edges[i][1];
7-
double s = succProb[i];
8-
g[a].push_back({b, s});
9-
g[b].push_back({a, s});
8+
double p = succProb[i];
9+
g[a].emplace_back(p, b);
10+
g[b].emplace_back(p, a);
1011
}
11-
vector<double> d(n);
12-
d[start] = 1.0;
13-
queue<pair<double, int>> q;
14-
q.push({1.0, start});
15-
while (!q.empty()) {
16-
auto p = q.front();
17-
q.pop();
18-
double w = p.first;
19-
int u = p.second;
20-
if (d[u] > w) continue;
21-
for (auto& e : g[u]) {
22-
int v = e.first;
23-
double t = e.second;
24-
if (d[v] < d[u] * t) {
25-
d[v] = d[u] * t;
26-
q.push({d[v], v});
12+
vector<double> dist(n);
13+
dist[start_node] = 1;
14+
priority_queue<pdi> pq;
15+
pq.emplace(1, start_node);
16+
while (!pq.empty()) {
17+
auto [w, a] = pq.top();
18+
pq.pop();
19+
if (dist[a] > w) {
20+
continue;
21+
}
22+
for (auto [p, b] : g[a]) {
23+
auto nw = w * p;
24+
if (nw > dist[b]) {
25+
dist[b] = nw;
26+
pq.emplace(nw, b);
2727
}
2828
}
2929
}
30-
return d[end];
30+
return dist[end_node];
3131
}
32-
};
32+
};
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
func maxProbability(n int, edges [][]int, succProb []float64, start int, end int) float64 {
1+
func maxProbability(n int, edges [][]int, succProb []float64, start_node int, end_node int) float64 {
22
g := make([][]pair, n)
33
for i, e := range edges {
4-
a, b, s := e[0], e[1], succProb[i]
5-
g[a] = append(g[a], pair{b, s})
6-
g[b] = append(g[b], pair{a, s})
4+
a, b := e[0], e[1]
5+
p := succProb[i]
6+
g[a] = append(g[a], pair{p, b})
7+
g[b] = append(g[b], pair{p, a})
78
}
8-
d := make([]float64, n)
9-
d[start] = 1
10-
vis := make([]bool, n)
11-
q := []int{start}
12-
vis[start] = true
13-
for len(q) > 0 {
14-
i := q[0]
15-
q = q[1:]
16-
vis[i] = false
17-
for _, ne := range g[i] {
18-
j, s := ne.idx, ne.s
19-
if d[j] < d[i]*s {
20-
d[j] = d[i] * s
21-
if !vis[j] {
22-
q = append(q, j)
23-
vis[j] = true
24-
}
9+
pq := hp{{1, start_node}}
10+
dist := make([]float64, n)
11+
dist[start_node] = 1
12+
for len(pq) > 0 {
13+
p := heap.Pop(&pq).(pair)
14+
w, a := p.p, p.a
15+
if dist[a] > w {
16+
continue
17+
}
18+
for _, e := range g[a] {
19+
b, p := e.a, e.p
20+
if nw := w * p; nw > dist[b] {
21+
dist[b] = nw
22+
heap.Push(&pq, pair{nw, b})
2523
}
2624
}
2725
}
28-
return d[end]
26+
return dist[end_node]
2927
}
3028

3129
type pair struct {
32-
idx int
33-
s float64
34-
}
30+
p float64
31+
a int
32+
}
33+
type hp []pair
34+
35+
func (h hp) Len() int { return len(h) }
36+
func (h hp) Less(i, j int) bool { return h[i].p > h[j].p }
37+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
38+
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
39+
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return }
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
class Solution {
2-
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
2+
public double maxProbability(
3+
int n, int[][] edges, double[] succProb, int start_node, int end_node) {
34
List<Pair<Integer, Double>>[] g = new List[n];
45
Arrays.setAll(g, k -> new ArrayList<>());
56
for (int i = 0; i < edges.length; ++i) {
6-
int a = edges[i][0], b = edges[i][1];
7-
double s = succProb[i];
8-
g[a].add(new Pair<>(b, s));
9-
g[b].add(new Pair<>(a, s));
7+
var e = edges[i];
8+
int a = e[0], b = e[1];
9+
double p = succProb[i];
10+
g[a].add(new Pair<>(b, p));
11+
g[b].add(new Pair<>(a, p));
1012
}
11-
PriorityQueue<Pair<Double, Integer>> q
12-
= new PriorityQueue<>(Comparator.comparingDouble(Pair::getKey));
13-
double[] d = new double[n];
14-
d[start] = 1.0;
15-
q.offer(new Pair<>(-1.0, start));
16-
while (!q.isEmpty()) {
17-
Pair<Double, Integer> p = q.poll();
18-
double w = p.getKey();
19-
w *= -1;
20-
int u = p.getValue();
21-
for (Pair<Integer, Double> ne : g[u]) {
22-
int v = ne.getKey();
23-
double t = ne.getValue();
24-
if (d[v] < d[u] * t) {
25-
d[v] = d[u] * t;
26-
q.offer(new Pair<>(-d[v], v));
13+
double[] dist = new double[n];
14+
dist[start_node] = 1;
15+
PriorityQueue<Pair<Integer, Double>> pq
16+
= new PriorityQueue<>(Comparator.comparingDouble(p -> - p.getValue()));
17+
pq.offer(new Pair<>(start_node, 1.0));
18+
while (!pq.isEmpty()) {
19+
var p = pq.poll();
20+
int a = p.getKey();
21+
double w = p.getValue();
22+
if (dist[a] > w) {
23+
continue;
24+
}
25+
for (var e : g[a]) {
26+
int b = e.getKey();
27+
double pab = e.getValue();
28+
double wab = w * pab;
29+
if (wab > dist[b]) {
30+
dist[b] = wab;
31+
pq.offer(new Pair<>(b, wab));
2732
}
2833
}
2934
}
30-
return d[end];
35+
return dist[end_node];
3136
}
32-
}
37+
}

solution/1500-1599/1514.Path with Maximum Probability/Solution.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ def maxProbability(
44
n: int,
55
edges: List[List[int]],
66
succProb: List[float],
7-
start: int,
8-
end: int,
7+
start_node: int,
8+
end_node: int,
99
) -> float:
10-
g = defaultdict(list)
11-
for (a, b), s in zip(edges, succProb):
12-
g[a].append((b, s))
13-
g[b].append((a, s))
14-
q = [(-1, start)]
15-
d = [0] * n
16-
d[start] = 1
17-
while q:
18-
w, u = heappop(q)
10+
g: List[List[Tuple[int, float]]] = [[] for _ in range(n)]
11+
for (a, b), p in zip(edges, succProb):
12+
g[a].append((b, p))
13+
g[b].append((a, p))
14+
pq = [(-1, start_node)]
15+
dist = [0] * n
16+
dist[start_node] = 1
17+
while pq:
18+
w, a = heappop(pq)
1919
w = -w
20-
if d[u] > w:
20+
if dist[a] > w:
2121
continue
22-
for v, t in g[u]:
23-
if d[v] < d[u] * t:
24-
d[v] = d[u] * t
25-
heappush(q, (-d[v], v))
26-
return d[end]
22+
for b, p in g[a]:
23+
if (t := w * p) > dist[b]:
24+
dist[b] = t
25+
heappush(pq, (-t, b))
26+
return dist[end_node]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function maxProbability(
2+
n: number,
3+
edges: number[][],
4+
succProb: number[],
5+
start_node: number,
6+
end_node: number,
7+
): number {
8+
const pq = new MaxPriorityQueue({ priority: v => v[0] });
9+
const g: [number, number][][] = Array.from({ length: n }, () => []);
10+
for (let i = 0; i < edges.length; ++i) {
11+
const [a, b] = edges[i];
12+
g[a].push([b, succProb[i]]);
13+
g[b].push([a, succProb[i]]);
14+
}
15+
const dist = Array.from({ length: n }, () => 0);
16+
dist[start_node] = 1;
17+
pq.enqueue([1, start_node]);
18+
while (!pq.isEmpty()) {
19+
const [w, a] = pq.dequeue().element;
20+
if (dist[a] > w) {
21+
continue;
22+
}
23+
for (const [b, p] of g[a]) {
24+
const nw = w * p;
25+
if (nw > dist[b]) {
26+
dist[b] = nw;
27+
pq.enqueue([nw, b]);
28+
}
29+
}
30+
}
31+
return dist[end_node];
32+
}

solution/1500-1599/1514.Path with Maximum Probability/Solution2.cpp

-34
This file was deleted.

solution/1500-1599/1514.Path with Maximum Probability/Solution2.java

-34
This file was deleted.

solution/1500-1599/1514.Path with Maximum Probability/Solution2.py

-28
This file was deleted.

solution/3200-3299/3266.Final Array State After K Multiplication Operations II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (h hp) Len() int { return len(h) }
325325
func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || h[i].x == h[j].x && h[i].i < h[j].i }
326326
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
327327
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
328-
func (h *hp) Pop() any { a := *h; x := a[len(a)-1]; *h = a[:len(a)-1]; return x }
328+
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return x }
329329
```
330330

331331
<!-- tabs:end -->

solution/3200-3299/3266.Final Array State After K Multiplication Operations II/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (h hp) Len() int { return len(h) }
323323
func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || h[i].x == h[j].x && h[i].i < h[j].i }
324324
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
325325
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
326-
func (h *hp) Pop() any { a := *h; x := a[len(a)-1]; *h = a[:len(a)-1]; return x }
326+
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return x }
327327
```
328328

329329
<!-- tabs:end -->

solution/3200-3299/3266.Final Array State After K Multiplication Operations II/Solution.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type pair struct{ x, i int }
4646
type hp []pair
4747

4848
func (h hp) Len() int { return len(h) }
49-
func (h hp) Less(i, j int) bool { return h[i].x < h[j].x }
49+
func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || h[i].x == h[j].x && h[i].i < h[j].i }
5050
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5151
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
52-
func (h *hp) Pop() (x any) { a := *h; x := a[len(a)-1]; *h = a[:len(a)-1]; return }
52+
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return x }

solution/3300-3399/3367.Maximize Sum of Weights after Edge Removals/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public:
186186
}
187187
}
188188
ranges::sort(t, greater<>());
189-
for (int i = 0; i < min((int)t.size(), k - 1); ++i) {
189+
for (int i = 0; i < min((int) t.size(), k - 1); ++i) {
190190
s += t[i];
191191
}
192192
return {s + (t.size() >= k ? t[k - 1] : 0), s};

0 commit comments

Comments
 (0)