Java in-place DP solution


  • 0
    K
    public class Solution {
        public int minPathSum(int[][] grid) {
            if(grid.length == 0 || grid[0].length == 0)
                return 0;
            for(int i = grid.length - 1; i >= 0; i--) {
                for(int j = grid[0].length - 1; j >= 0; j--) {
                    if(i == grid.length - 1 && j == grid[0].length - 1)
                        grid[i][j] = grid[i][j];
                    else if(i == grid.length - 1)
                        grid[i][j] = grid[i][j] + grid[i][j + 1];
                    else if(j == grid[0].length - 1)
                        grid[i][j] = grid[i][j] + grid[i + 1][j];
                    else
                        grid[i][j] = grid[i][j] + Math.min(grid[i][j + 1], 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.