Skip to content

feat: add solutions to lc problem: No.1514 #3816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 133 additions & 201 deletions solution/1500-1599/1514.Path with Maximum Probability/README.md

Large diffs are not rendered by default.

334 changes: 135 additions & 199 deletions solution/1500-1599/1514.Path with Maximum Probability/README_EN.md

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions solution/1500-1599/1514.Path with Maximum Probability/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
class Solution {
public:
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
vector<vector<pair<int, double>>> g(n);
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
using pdi = pair<double, int>;
vector<pdi> g[n];
for (int i = 0; i < edges.size(); ++i) {
int a = edges[i][0], b = edges[i][1];
double s = succProb[i];
g[a].push_back({b, s});
g[b].push_back({a, s});
double p = succProb[i];
g[a].emplace_back(p, b);
g[b].emplace_back(p, a);
}
vector<double> d(n);
d[start] = 1.0;
queue<pair<double, int>> q;
q.push({1.0, start});
while (!q.empty()) {
auto p = q.front();
q.pop();
double w = p.first;
int u = p.second;
if (d[u] > w) continue;
for (auto& e : g[u]) {
int v = e.first;
double t = e.second;
if (d[v] < d[u] * t) {
d[v] = d[u] * t;
q.push({d[v], v});
vector<double> dist(n);
dist[start_node] = 1;
priority_queue<pdi> pq;
pq.emplace(1, start_node);
while (!pq.empty()) {
auto [w, a] = pq.top();
pq.pop();
if (dist[a] > w) {
continue;
}
for (auto [p, b] : g[a]) {
auto nw = w * p;
if (nw > dist[b]) {
dist[b] = nw;
pq.emplace(nw, b);
}
}
}
return d[end];
return dist[end_node];
}
};
};
55 changes: 30 additions & 25 deletions solution/1500-1599/1514.Path with Maximum Probability/Solution.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
func maxProbability(n int, edges [][]int, succProb []float64, start int, end int) float64 {
func maxProbability(n int, edges [][]int, succProb []float64, start_node int, end_node int) float64 {
g := make([][]pair, n)
for i, e := range edges {
a, b, s := e[0], e[1], succProb[i]
g[a] = append(g[a], pair{b, s})
g[b] = append(g[b], pair{a, s})
a, b := e[0], e[1]
p := succProb[i]
g[a] = append(g[a], pair{p, b})
g[b] = append(g[b], pair{p, a})
}
d := make([]float64, n)
d[start] = 1
vis := make([]bool, n)
q := []int{start}
vis[start] = true
for len(q) > 0 {
i := q[0]
q = q[1:]
vis[i] = false
for _, ne := range g[i] {
j, s := ne.idx, ne.s
if d[j] < d[i]*s {
d[j] = d[i] * s
if !vis[j] {
q = append(q, j)
vis[j] = true
}
pq := hp{{1, start_node}}
dist := make([]float64, n)
dist[start_node] = 1
for len(pq) > 0 {
p := heap.Pop(&pq).(pair)
w, a := p.p, p.a
if dist[a] > w {
continue
}
for _, e := range g[a] {
b, p := e.a, e.p
if nw := w * p; nw > dist[b] {
dist[b] = nw
heap.Push(&pq, pair{nw, b})
}
}
}
return d[end]
return dist[end_node]
}

type pair struct {
idx int
s float64
}
p float64
a int
}
type hp []pair

func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].p > h[j].p }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return }
51 changes: 28 additions & 23 deletions solution/1500-1599/1514.Path with Maximum Probability/Solution.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
class Solution {
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
public double maxProbability(
int n, int[][] edges, double[] succProb, int start_node, int end_node) {
List<Pair<Integer, Double>>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int i = 0; i < edges.length; ++i) {
int a = edges[i][0], b = edges[i][1];
double s = succProb[i];
g[a].add(new Pair<>(b, s));
g[b].add(new Pair<>(a, s));
var e = edges[i];
int a = e[0], b = e[1];
double p = succProb[i];
g[a].add(new Pair<>(b, p));
g[b].add(new Pair<>(a, p));
}
PriorityQueue<Pair<Double, Integer>> q
= new PriorityQueue<>(Comparator.comparingDouble(Pair::getKey));
double[] d = new double[n];
d[start] = 1.0;
q.offer(new Pair<>(-1.0, start));
while (!q.isEmpty()) {
Pair<Double, Integer> p = q.poll();
double w = p.getKey();
w *= -1;
int u = p.getValue();
for (Pair<Integer, Double> ne : g[u]) {
int v = ne.getKey();
double t = ne.getValue();
if (d[v] < d[u] * t) {
d[v] = d[u] * t;
q.offer(new Pair<>(-d[v], v));
double[] dist = new double[n];
dist[start_node] = 1;
PriorityQueue<Pair<Integer, Double>> pq
= new PriorityQueue<>(Comparator.comparingDouble(p -> - p.getValue()));
pq.offer(new Pair<>(start_node, 1.0));
while (!pq.isEmpty()) {
var p = pq.poll();
int a = p.getKey();
double w = p.getValue();
if (dist[a] > w) {
continue;
}
for (var e : g[a]) {
int b = e.getKey();
double pab = e.getValue();
double wab = w * pab;
if (wab > dist[b]) {
dist[b] = wab;
pq.offer(new Pair<>(b, wab));
}
}
}
return d[end];
return dist[end_node];
}
}
}
34 changes: 17 additions & 17 deletions solution/1500-1599/1514.Path with Maximum Probability/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ def maxProbability(
n: int,
edges: List[List[int]],
succProb: List[float],
start: int,
end: int,
start_node: int,
end_node: int,
) -> float:
g = defaultdict(list)
for (a, b), s in zip(edges, succProb):
g[a].append((b, s))
g[b].append((a, s))
q = [(-1, start)]
d = [0] * n
d[start] = 1
while q:
w, u = heappop(q)
g: List[List[Tuple[int, float]]] = [[] for _ in range(n)]
for (a, b), p in zip(edges, succProb):
g[a].append((b, p))
g[b].append((a, p))
pq = [(-1, start_node)]
dist = [0] * n
dist[start_node] = 1
while pq:
w, a = heappop(pq)
w = -w
if d[u] > w:
if dist[a] > w:
continue
for v, t in g[u]:
if d[v] < d[u] * t:
d[v] = d[u] * t
heappush(q, (-d[v], v))
return d[end]
for b, p in g[a]:
if (t := w * p) > dist[b]:
dist[b] = t
heappush(pq, (-t, b))
return dist[end_node]
32 changes: 32 additions & 0 deletions solution/1500-1599/1514.Path with Maximum Probability/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function maxProbability(
n: number,
edges: number[][],
succProb: number[],
start_node: number,
end_node: number,
): number {
const pq = new MaxPriorityQueue({ priority: v => v[0] });
const g: [number, number][][] = Array.from({ length: n }, () => []);
for (let i = 0; i < edges.length; ++i) {
const [a, b] = edges[i];
g[a].push([b, succProb[i]]);
g[b].push([a, succProb[i]]);
}
const dist = Array.from({ length: n }, () => 0);
dist[start_node] = 1;
pq.enqueue([1, start_node]);
while (!pq.isEmpty()) {
const [w, a] = pq.dequeue().element;
if (dist[a] > w) {
continue;
}
for (const [b, p] of g[a]) {
const nw = w * p;
if (nw > dist[b]) {
dist[b] = nw;
pq.enqueue([nw, b]);
}
}
}
return dist[end_node];
}

This file was deleted.

This file was deleted.

28 changes: 0 additions & 28 deletions solution/1500-1599/1514.Path with Maximum Probability/Solution2.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (h hp) Len() int { return len(h) }
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 }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
func (h *hp) Pop() any { a := *h; x := a[len(a)-1]; *h = a[:len(a)-1]; return x }
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return x }
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (h hp) Len() int { return len(h) }
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 }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
func (h *hp) Pop() any { a := *h; x := a[len(a)-1]; *h = a[:len(a)-1]; return x }
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return x }
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type pair struct{ x, i int }
type hp []pair

func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].x < h[j].x }
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 }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
func (h *hp) Pop() (x any) { a := *h; x := a[len(a)-1]; *h = a[:len(a)-1]; return }
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return x }
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public:
}
}
ranges::sort(t, greater<>());
for (int i = 0; i < min((int)t.size(), k - 1); ++i) {
for (int i = 0; i < min((int) t.size(), k - 1); ++i) {
s += t[i];
}
return {s + (t.size() >= k ? t[k - 1] : 0), s};
Expand Down
Loading