304 North Cardinal St.
Dorchester Center, MA 02124

# Minimum Path Sum LeetCode Solution

## Problem – Minimum Path Sum LeetCode Solution

Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example 1: ``````Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
``````

Example 2:

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

Constraints:

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

## Minimum Path Sum LeetCode Solution in Python

``````def minPathSum(self, grid):
m = len(grid)
n = len(grid)
for i in range(1, n):
grid[i] += grid[i-1]
for i in range(1, m):
grid[i] += grid[i-1]
for i in range(1, m):
for j in range(1, n):
grid[i][j] += min(grid[i-1][j], grid[i][j-1])
return grid[-1][-1]
``````

## Minimum Path Sum LeetCode Solution in C++

``````class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid.size();
vector<vector<int> > sum(m, vector<int>(n, grid));
for (int i = 1; i < m; i++)
sum[i] = sum[i - 1] + grid[i];
for (int j = 1; j < n; j++)
sum[j] = sum[j - 1] + grid[j];
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
sum[i][j]  = min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j];
return sum[m - 1][n - 1];
}
};
``````

## Minimum Path Sum LeetCode Solution in Java

``````public static int minPathSum(int[][] grid) {

int height = grid.length;
int width = grid.length;
return min(grid, height - 1, width - 1);

}

public static int min(int[][]grid, int row, int col){

if(row == 0 && col == 0) return grid[row][col]; // this is the exit of the recursion
if(row == 0) return grid[row][col] + min(grid, row, col - 1); /** when we reach the first row, we could only move horizontally.*/
if(col == 0) return grid[row][col] + min(grid, row - 1, col); /** when we reach the first column, we could only move vertically.*/
return grid[row][col] + Math.min(min(grid, row - 1, col), min(grid, row, col - 1)); /** we want the min sum path so we pick the cell with the less value */

}
``````
##### Minimum Path Sum LeetCode Solution Review:

In our experience, we suggest you solve this Minimum Path Sum LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Path Sum LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Minimum Path Sum LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions