```
public class Solution {
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] dp = new int[n];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for(int i=0; i<m; i++){
dp[0] += grid[i][0];
for(int j=1; j<n; j++)
dp[j] = Math.min(dp[j-1], dp[j]) + grid[i][j];
}
return dp[n-1];
}
}
```