Skip to the code
+10 XP

#466 All-Pairs Travel Time Table

easy

An office-planning dashboard shows a table of walking times between every pair of n rooms, numbered 0 to n - 1. Each paths[i] = [from, to, minutes] is a one-way corridor (some doors only open one way).

Write travelTable(n, paths) that returns an n by n table where entry [i][j] is the fewest minutes to walk from room i to room j, 0 when i === j, and -1 if room j can't be reached from room i.

Example 1

Input: n = 3, paths = [[0,1,4],[1,2,1],[0,2,7],[2,0,2]]
Output: [[0,4,5],[3,0,1],[2,6,0]]

For example, room 0 to room 2 is faster through room 1 (4 + 1 = 5) than directly (7), and room 1 to room 0 goes through room 2 (1 + 2 = 3).

Example 2

Input: n = 2, paths = []
Output: [[0,-1],[-1,0]]

No corridors, so each room only reaches itself.

Constraints

  • 1 <= n <= 100
  • 0 <= paths.length <= n * n, so at most 10,000
  • 1 <= minutes <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).