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:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
public int minPathSum(int[][] o) {
int[][] d = new int[o.length][o[0].length];
for(int i = 0; i < o.length; i++){
for(int j = 0; j < o[0].length ; j++){
d[i][j] = o[i][j];
if(i == 0 && j==0)continue;
else if(i == 0)d[i][j] += d[i][j-1];
else if(j == 0)d[i][j] += d[i-1][j];
else{
d[i][j] += Math.min(d[i][j-1], d[i-1][j]);
}
}
}
return d[o.length - 1][o[0].length - 1];
}