Skip to content
Onsite

Minimum Path Sum

MediumP2Coding

grid is an m x n grid of non-negative integers. Starting at the top-left cell, move only right or down until you reach the bottom-right cell. The cost of a path is the sum of every cell it visits, including the first and last.

Return the smallest possible cost.

Example 1

Input
grid = [[1,3,1],[1,5,1],[4,2,1]]
Output
7
Why
The path 1 -> 3 -> 1 -> 1 -> 1 costs 7.

Example 2

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

Example 3

Input
grid = [[7]]
Output
7

Constraints

  • 1 <= m, n <= 200
  • 0 <= grid[i][j] <= 100

Hints

Run checks the 3 visible tests, Submit all 10.