Fastest Commute Mode
Asked at Databricks
Reported by the problem's creator, not verified
A city map is a list of equal-length strings, one per row. It holds exactly one S (the start) and one D (the destination). Every other cell is X (blocked) or a digit from 1 to 4, a road for the travel mode with that number.
A trip moves one step at a time to an edge-adjacent cell (up, down, left or right; no diagonals). A trip in mode m may only step on cells marked m, plus S and D.
Examples
Example 1
Input: grid =
["S11X","2X1X","2X1D","2222"], time =[4,2,1,1], cost =[1,3,1,1]Output:
2Explanation: Mode 1 takes 5 steps along the top row and down the
1column: time 5 4 = 20, cost 5. Mode 2 takes 7 steps down the left side and along the bottom row: time 7 2 = 14, cost 21. Modes 3 and 4 have no cells and cannot reachD. Time decides first, so the answer is 2.