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.
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).
Input: n = 2, paths = [] Output: [[0,-1],[-1,0]]
No corridors, so each room only reaches itself.
1 <= n <= 1000 <= paths.length <= n * n, so at most 10,0001 <= minutes <= 1000Run 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).
I'm here to help with #466 All-Pairs Travel Time Table. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.