Ctrl K

Most Reliable Relay Route

Medium
A network has n relays numbered 0 to n - 1. The list links describes two-way connections: links[i] = [a, b] joins relays a and b, and chances[i] is the probability that a message crosses that link intact. Failures on different links are independent, so the chance of getting through a whole route is the product of its links' chances. Return the highest chance of delivering a message from relay source to relay target. If no route connects them, return 0. An answer within 10⁻⁵ of the correct one is accepted.
Example 1
n=4, links=[[0, 1], [1, 3], [0, 2], [2, 3]], chances=[0.9, 0.5, 0.6, 0.8], source=0, target=3 → 0.48

Through node 1 the message gets through with chance 0.9 · 0.5 = 0.45, through node 2 with 0.6 · 0.8 = 0.48. The second route wins.

Example 2
n=3, links=[[0, 1]], chances=[1.0], source=0, target=2 → 0

No link reaches node 2 at all, so the chance is 0.

Example 3
n=3, links=[[0, 1], [1, 2], [0, 2]], chances=[0.7, 0.7, 0.4], source=2, target=0 → 0.48999999999999994
Constraints
  • 2 ≤ n ≤ 10⁴
  • 0 ≤ source, target < n, source ≠ target
  • 0 ≤ len(links) = len(chances) ≤ 2 · 10⁴
  • 0 ≤ a, b < n, a ≠ b
  • 0 ≤ chances[i] ≤ 1
  • Any two relays share at most one link
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Path of a Rolling Ball MediumSouvenir Round Trip MediumA Trip Through Tunnels MediumNearest Flagged Node MediumStops That Close Medium
loading Python…
Input
n=4, links=[[0, 1], [1, 3], [0, 2], [2, 3]], chances=[0.9, 0.5, 0.6, 0.8], source=0, target=3
Expected
0.48

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.