Find the minimum path cost from top-left to bottom-right in a grid with four-direction moves

Quick Overview

Given a grid of positive cell costs, find the minimum total cost of a path from the top-left cell to the bottom-right cell when moves can go up, down, left or right. It tests Dijkstra's algorithm on a grid and recognizing when a right-and-down dynamic program no longer applies.

Find the minimum path cost from top-left to bottom-right in a grid with four-direction moves

Company: Algolia

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a 2D matrix `grid` of size `m x n` where each cell contains a positive integer representing the cost of entering that cell, find the minimum total cost to travel from the top-left cell `(0, 0)` to the bottom-right cell `(m-1, n-1)`. From any cell, you can move in four directions: up, down, left, or right. The total cost of a path is the sum of the costs of all cells visited along the path, including both the starting and ending cells. Return the minimum total cost required to reach the bottom-right cell. ### Function Signature ```python def min_path_cost(grid: list[list[int]]) -> int: ``` ### Rules - Moves must stay inside the grid. There are no blocked cells. - The starting cell's cost is always paid, so a `1 x 1` grid returns `grid[0][0]`. ### Constraints - `m == len(grid)` - `n == len(grid[i])` - `1 <= m, n <= 100` - `1 <= grid[i][j] <= 1000` ### Examples ```text Input: grid = [[1,3,1],[1,5,1],[4,2,1]] Output: 7 ``` The cheapest path is 1 -> 3 -> 1 -> 1 -> 1: along the top row, then down the right column. Going down the left column first costs 1 + 1 + 4 + 2 + 1 = 9. ```text Input: grid = [[1,2,3],[4,5,6]] Output: 12 ``` The cheapest path is 1 -> 2 -> 3 -> 6. The alternatives 1 -> 2 -> 5 -> 6 and 1 -> 4 -> 5 -> 6 cost 14 and 16. ```text Input: grid = [[1,1,1],[9,9,1],[1,1,1],[1,9,9],[1,1,1]] Output: 11 ``` The cheapest path goes right along row 0, down to row 2, left along row 2, down the left column and right along the bottom row. It visits 11 cells of cost 1 and avoids every 9. Any path that only moves right and down must enter a 9 and costs at least 15.

Overview: Given a grid of positive cell costs, find the minimum total cost of a path from the top-left cell to the bottom-right cell when moves can go up, down, left or right. It tests Dijkstra's algorithm on a grid and recognizing when a right-and-down dynamic program no longer applies.

Algolia logo
Algolia
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a 2D matrix grid of size m x n where each cell contains a positive integer representing the cost of entering that cell, find the minimum total cost to travel from the top-left cell (0, 0) to the bottom-right cell (m-1, n-1).

From any cell, you can move in four directions: up, down, left, or right. The total cost of a path is the sum of the costs of all cells visited along the path, including both the starting and ending cells.

Return the minimum total cost required to reach the bottom-right cell.

Function Signature

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

Rules

  • Moves must stay inside the grid. There are no blocked cells.
  • The starting cell's cost is always paid, so a 1 x 1 grid returns grid[0][0] .

Constraints

  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 100
  • 1 <= grid[i][j] <= 1000

Examples

Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7

The cheapest path is 1 -> 3 -> 1 -> 1 -> 1: along the top row, then down the right column. Going down the left column first costs 1 + 1 + 4 + 2 + 1 = 9.

Input: grid = [[1,2,3],[4,5,6]]
Output: 12

The cheapest path is 1 -> 2 -> 3 -> 6. The alternatives 1 -> 2 -> 5 -> 6 and 1 -> 4 -> 5 -> 6 cost 14 and 16.

Input: grid = [[1,1,1],[9,9,1],[1,1,1],[1,9,9],[1,1,1]]
Output: 11

The cheapest path goes right along row 0, down to row 2, left along row 2, down the left column and right along the bottom row. It visits 11 cells of cost 1 and avoids every 9. Any path that only moves right and down must enter a 9 and costs at least 15.