5ms ... constant space DP solution .. java .. dont see how i cud optimize further


  • 0
    K

    '''
    public class Solution {

    public int minPathSum(int[][] grid) {
        
        int m = grid.length;
        int n= grid[0].length;
        
        for(int i=m-1 ;i >=0 ; i--)
        {
            for(int j=n-1 ;j>=0 ; j--)
            {
                if(j+1<n && i+1<m)
                {
                    grid[i][j]+=Math.min(grid[i][j+1],grid[i+1][j]);
                }
                else if(j+1<n)
                {
                    grid[i][j]+=grid[i][j+1];
                }
                else if(i+1<m)
                {
                    grid[i][j]+=grid[i+1][j];
                }
            }
        }
        
        
        return grid[0][0];
    }
    

    }
    '''


Log in to reply
 

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