Skip to interview prep
Apexed
Problem list140 / 143

140. Route Within a Relay Budget

HardWeighted graphs

Nodes are numbered from 0 to count - 1. Each directed link [from, to, latency, uses_relay] adds its latency and consumes one credit if uses_relay is true.

Return the least total latency from source to target using at most budget credits, or -1 if unreachable. The empty route from a node to itself has latency zero.

Example 1

Input: count = 4, links = [[0,1,1,true],[1,3,1,true],[0,2,3,false],[2,3,3,false],[1,2,1,false]], source = 0, target = 3, budget = 1
Output: 5

Example 2

Input: count = 3, links = [[0,1,0,true],[0,1,4,false],[1,2,2,true]], source = 0, target = 2, budget = 1
Output: 6

Constraints

  • 1 ≤ count ≤ 2,000, at most 10,000 links, and 0 ≤ budget ≤ 8.
  • Latencies are integers in [0, 10⁶]. Links can repeat or form cycles.

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 6 hidden and sample tests.

Code runs in an isolated browser sandbox. Progress and submissions are saved on this device.

Route Within a Relay Budget — coding interview practice — Apexed