Minimum commute time from top-left to bottom-right on a grid of transportation modes

Quick Overview

Find the minimum total time to travel from the top-left to the bottom-right corner of a grid where each cell's transportation mode (walk, bike, car or train) sets the time to pass through it, counting the starting cell. It tests modeling a grid as a weighted graph and running Dijkstra's algorithm.

Minimum commute time from top-left to bottom-right on a grid of transportation modes

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an `m x n` grid where each cell contains an integer representing a transportation mode. Each mode has a different time cost to traverse that cell. Find the minimum total time to travel from the top-left corner `(0, 0)` to the bottom-right corner `(m-1, n-1)`. You can move in four directions: up, down, left and right. Transportation modes and their time costs: - `0` = Walk (cost `10`) - `1` = Bike (cost `5`) - `2` = Car (cost `2`) - `3` = Train (cost `1`) The time cost to enter a cell is determined by the transportation mode of that destination cell. The total time includes the cost of the starting cell. ### Function Signature ```python def optimal_commute(grid: list[list[int]]) -> int: ``` ### Rules - `grid` is a list of rows, and each row is a list of integers from 0 to 3. - The total is the sum of the mode costs of every cell on the path, including both the start cell `(0, 0)` and the end cell `(m-1, n-1)`. - Moves go to one of the four neighboring cells. There are no diagonal moves and no moves outside the grid. - Every cell can be entered, so the bottom-right corner is always reachable. - For a 1 x 1 grid the start and the end are the same cell, and the answer is that cell's cost. ### Constraints - `1 <= m, n <= 200` - `0 <= grid[i][j] <= 3` - The grid always has at least one cell. ### Examples ```text Input: grid = [[0,1,2],[3,0,1],[2,3,3]] Output: 15 ``` The path `(0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2)` costs 10 + 1 + 2 + 1 + 1 = 15, where the first 10 is the Walk cell you start on. The top-row route `(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2)` costs 10 + 5 + 2 + 5 + 1 = 23, which is worse. ```text Input: grid = [[3,3],[3,3]] Output: 3 ``` All cells are Train (cost 1). Any shortest path from `(0,0)` to `(1,1)` passes through 3 cells, so the minimum time is 1 + 1 + 1 = 3. ```text Input: grid = [[0]] Output: 10 ``` The start is also the destination. No move is made, but the start cell is counted, so the answer is the Walk cost of 10.

Overview: Find the minimum total time to travel from the top-left to the bottom-right corner of a grid where each cell's transportation mode (walk, bike, car or train) sets the time to pass through it, counting the starting cell. It tests modeling a grid as a weighted graph and running Dijkstra's algorithm.

Databricks logo
Databricks
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an m x n grid where each cell contains an integer representing a transportation mode. Each mode has a different time cost to traverse that cell. Find the minimum total time to travel from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1).

You can move in four directions: up, down, left and right.

Transportation modes and their time costs:

  • 0 = Walk (cost 10 )
  • 1 = Bike (cost 5 )
  • 2 = Car (cost 2 )
  • 3 = Train (cost 1 )

The time cost to enter a cell is determined by the transportation mode of that destination cell. The total time includes the cost of the starting cell.

Function Signature

def optimal_commute(grid: list[list[int]]) -> int:

Rules

  • grid is a list of rows, and each row is a list of integers from 0 to 3.
  • The total is the sum of the mode costs of every cell on the path, including both the start cell (0, 0) and the end cell (m-1, n-1) .
  • Moves go to one of the four neighboring cells. There are no diagonal moves and no moves outside the grid.
  • Every cell can be entered, so the bottom-right corner is always reachable.
  • For a 1 x 1 grid the start and the end are the same cell, and the answer is that cell's cost.

Constraints

  • 1 <= m, n <= 200
  • 0 <= grid[i][j] <= 3
  • The grid always has at least one cell.

Examples

Input: grid = [[0,1,2],[3,0,1],[2,3,3]]
Output: 15

The path (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2) costs 10 + 1 + 2 + 1 + 1 = 15, where the first 10 is the Walk cell you start on. The top-row route (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) costs 10 + 5 + 2 + 5 + 1 = 23, which is worse.

Input: grid = [[3,3],[3,3]]
Output: 3

All cells are Train (cost 1). Any shortest path from (0,0) to (1,1) passes through 3 cells, so the minimum time is 1 + 1 + 1 = 3.

Input: grid = [[0]]
Output: 10

The start is also the destination. No move is made, but the start cell is counted, so the answer is the Walk cost of 10.