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.
public class Solution {
public int minPathSum(int[][] grid) {
int len1 = grid.length;
if(len1 == 0) return 0;
int len2 = grid[0].length;
if(len2 == 0) return 0;
int f[] = new int[len2];
for(int i = 0; i<len1; i++){
f[0] += grid[i][0];
for(int j = 1; j<len2; j++){
if(i == 0){
f[j] = f[j-1] + grid[0][j];
}else{
f[j] = Math.min(f[j-1], f[j]) + grid[i][j];
}
}
}
return f[len2-1];
}
}