Skip to the code
cf.
+50 XP

#558 Shortest Tour Through Every Screen

hard

A product tour visits every screen of an app exactly once, starting on screen 0. time[i][j] is how many seconds it takes to go from screen i to screen j (it may differ from time[j][i]). The tour does not need to return to screen 0.

Write shortestTour(time) that returns the least total time of a tour that starts at screen 0 and visits every screen exactly once.

Example 1

Input: time = [[0,2,9,10],[1,0,6,4],[15,7,0,8],[6,3,12,0]]
Output: 16

0 → 1 → 2 → 3 takes 2 + 6 + 8 = 16.

Example 2

Input: time = [[0]]
Output: 0

The tour is already done.

Constraints

  • 1 <= n <= 12
  • time.length === n and every row has length n
  • time[i][i] === 0, and 0 <= time[i][j] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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