-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1514-path-with-maximum-probability.kt
35 lines (29 loc) · 1.12 KB
/
1514-path-with-maximum-probability.kt
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
29
30
31
32
33
34
35
class Solution {
fun maxProbability(n: Int, edges: Array<IntArray>, succProb: DoubleArray, start: Int, end: Int): Double {
val adj = HashMap<Int, ArrayList<Pair<Int, Double>>>().apply {
for ((i, edge) in edges.withIndex()) {
val (u, v) = edge
this[u] = getOrDefault(u, ArrayList<Pair<Int, Double>>()).apply { add(v to succProb[i]) }
this[v] = getOrDefault(v, ArrayList<Pair<Int, Double>>()).apply { add(u to succProb[i]) }
}
}
val visited = HashSet<Int>()
val minHeap = PriorityQueue<Pair<Int, Double>>{ a, b ->
if (a.second < b.second) 1 else -1
}
with (minHeap) {
add(start to 1.0)
while (isNotEmpty()) {
val (node, currProb) = poll()
visited.add(node)
if (node == end) return currProb
adj[node]?.forEach {
if (it.first !in visited) {
add(it.first to currProb * it.second)
}
}
}
}
return 0.0
}
}