Fastest Commute Mode

Asked at DatabricksOnsite

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

  1. Example 1

    Input: grid = ["S11X","2X1X","2X1D","2222"], time = [4,2,1,1], cost = [1,3,1,1]

    Output: 2

    Explanation: Mode 1 takes 5 steps along the top row and down the 1 column: 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 reach D. Time decides first, so the answer is 2.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.