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 m = grid.length; int n = grid[0].length; int[][] dp = new int[m][n]; for(int i = 0 ; i < m ; i++){ for(int j = 0 ; j < n ; j++){ if(i==0 && j==0) dp[0][0] = grid[0][0];//take care the first value else{ dp[i][j] = grid[i][j] + Math.min((i>0?dp[i-1][j]:Integer.MAX_VALUE),(j>0?dp[i][j-1]:Integer.MAX_VALUE)); } } } return dp[m-1][n-1]; } }
mistake: in dp loop, grid[i][j] is the value to start to calculate