4ms java DP solution


  • 0
    L
    public int minPathSum(int[][] grid) {
        if (grid.length == 0) {
            return 0;
        }
        int height = grid.length;
        int width = grid[0].length;
        for (int i = width-2; i >= 0; i--) {
            grid[height-1][i] += grid[height-1][i+1];
        }
        for (int i = height-2; i >= 0; i--) {
            grid[i][width-1] += grid[i+1][width-1];
        }
        for (int i = width-2; i >= 0; i--) {
            for (int j = height -2; j >= 0; j--) {
                grid[j][i] += Math.min(grid[j+1][i], grid[j][i+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.