# Leetcode 64 minimum path sum

2020-11-16 20:32:04

## LeetCode64 Minimum path sum

### Title Description

Given a... That contains a nonnegative integer `*m* x *n*` grid `grid` , Please find a path from the top left corner to the bottom right corner , Make the sum of the numbers on the path the smallest .

explain ： You can only move down or right one step at a time .

### Examples

`````` Input ：grid = [[1,3,1],[1,5,1],[4,2,1]]
Output ：7
explain ： Because the path  1→3→1→1→1  The sum of is the smallest .
``````
`````` Input ：grid = [[1,2,3],[4,5,6]]
Output ：12
``````

### Algorithm analysis

• f[i][j] From the beginning to the beginning i,j The minimum path of and

\(O(nm)\)

### Java Code

``````class Solution {
public int minPathSum(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int[][] f = new int[n+10][m+10];
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j++){
f[i][j] = 0x3f3f3f;
}
}
f[0][0] = grid[0][0];

for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j++){
if(i - 1 >= 0) f[i][j] = Math.min(f[i-1][j]+grid[i][j],f[i][j]);
if(j - 1 >= 0) f[i][j] = Math.min(f[i][j-1]+grid[i][j],f[i][j]);
}
}

return f[n-1][m-1];

}
}
``````