Ctrl K

Courier's Way Home

Medium
A city is a grid of m rows and n columns of blocks; rows are numbered from 0 at the top and columns from 0 at the left. A courier stands at block start = [row, column] and lives at block home. In one step the courier moves to a neighboring block up, down, left or right, without leaving the grid. Every step has a toll: - a step up or down into a block in row r costs row_tolls[r]; - a step left or right into a block in column c costs col_tolls[c]. Here m = len(row_tolls) and n = len(col_tolls). Return the minimum cost for the courier to get home.
Example 1
start=[0, 2], home=[2, 0], row_tolls=[9, 1, 4], col_tolls=[3, 6, 2] → 14

Two steps down enter rows 1 and 2 for 1 + 4. Two steps left enter columns 1 and 0 for 6 + 3. Total 14, and any other order of the same steps costs the same.

Example 2
start=[1, 1], home=[1, 1], row_tolls=[5, 5], col_tolls=[5, 5] → 0

The courier is already home, so there is nothing to pay.

Example 3
start=[3, 0], home=[0, 1], row_tolls=[2, 0, 7, 1], col_tolls=[8, 5] → 14
Constraints
  • 1 ≤ len(row_tolls), len(col_tolls) ≤ 10⁵
  • 0 ≤ row_tolls[r], col_tolls[c] ≤ 10⁴
  • len(start) = len(home) = 2
  • 0 ≤ start[0], home[0] < len(row_tolls)
  • 0 ≤ start[1], home[1] < len(col_tolls)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumFewest Jumps to the End MediumReach the Last Cell MediumAs Many Trades as You Like MediumWhere to Start the Lap Medium
loading Python…
Input
start=[0, 2], home=[2, 0], row_tolls=[9, 1, 4], col_tolls=[3, 6, 2]
Expected
14

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.