Minimum Cost Path in Weighted Grid

Given a grid of non-negative costs, return the minimum total cost required to travel from the top-left cell to the bottom-right cell, moving only in four directions. Pattern focus: Weighted shortest path. The cost accumulates as you move, so shortest path logic must consider edge weights explicitly.

Input Format

grid = non-negative cost grid

Output Format

minimum path cost from start to end

Constraints

  • 1 <= rows, cols <= 100
  • 0 <= grid[i][j] <= 10^6

Examples

Example 1:

Input:

grid = [[1,3,1],[1,5,1],[4,2,1]]

Output:

7

Explanation:

The cheapest route avoids the larger costs.

Example 2:

Input:

grid = [[1,2,3],[4,5,6]]

Output:

12

Explanation:

The path cost is the sum of the chosen cells.

Example 3:

Input:

grid = [[5]]

Output:

5

Explanation:

A single cell contributes its own cost.

Loading...
Minimum Cost Path in Weighted Grid