28ms c++ easy understand solution


  • 0
    P
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        for (int i = n-2; i >= 0; --i) {
            grid[m-1][i] += grid[m-1][i+1];
        }
        for (int i = m-2; i >= 0; --i) {
            grid[i][n-1] += grid[i+1][n-1];
        }
        for (int i = m-2; i >=0; --i) {
            for (int j = n-2; j >= 0; --j) {
                grid[i][j] += min(grid[i+1][j], grid[i][j+1]);
            }
        }
        return grid[0][0];
    }

Log in to reply
 

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