9ms cpp solution


  • 0
    G
    class Solution {
    public:
        int minPathSum(vector<vector<int>>& grid) {
            int row=grid.size(),col=grid[0].size();
            for(int i=row-1;i>=0;i--)
            {
                for(int j=col-1;j>=0;j--)
                {
                    if(i==row-1&&j==col-1)//last point
                        grid[i][j]=grid[i][j];
                    if(j==col-1&&i<row-1)//last column
                        grid[i][j]+=grid[i+1][j];
                    if(i==row-1&&j<col-1)//last row
                        grid[i][j]+=grid[i][j+1];
                    if(i<row-1&&j<col-1)//others
                        grid[i][j]=min((grid[i][j]+grid[i][j+1]),(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.