My DP solution in Java


  • 0
    Z

    public class Solution {

    public int minPathSum(int[][] grid) {
        int l= grid.length;
        int w=grid[0].length;
        for(int i=0;i<l;i++){
            for(int j=0;j<w;j++){
                if(i==0&&j==0){
                    
                }
                else if(i==0){
                    grid[0][j]+=grid[0][j-1];
                }
                else if(j==0){
                    grid[i][0]+=grid[i-1][0];
                }
                else
                grid[i][j]+=  grid[i-1][j]>grid[i][j-1]?grid[i][j-1]:grid[i-1][j];
            }
        }
        return grid[l-1][w-1];
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.