63. Unique Paths II and 64. Minimum Path Sum


This time the topic of brushing dp is very easy.

1 63 Unique Paths II

1.1 Title description

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and space is marked as 1 and 0 respectively in the grid.

Input: integer array grid, representing an mxn grid, grid[i][j]=1 means obstacles and cannot be passed; grid[i][j]=0 means passable.
Output: the number of unique paths that
can go from the upper left corner to the lower right corner Rule: Only go down or to the right

Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output: 2
Explanation: There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:

  1. Right -> Right -> Down -> Down
  2. Down -> Down -> Right -> Right

1.2 Dynamic programming solution

class Solution {
    
    
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    
    
        if(obstacleGrid[0][0]==1) return 0;
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        dp[0][0] = (obstacleGrid[0][0]==1?0:1);
        for(int j=1;j<n;j++){
    
    
            dp[0][j]= (obstacleGrid[0][j]==1?0:dp[0][j-1]);
        }
        for(int i=1;i<m;i++){
    
    
            dp[i][0] = (obstacleGrid[i][0]==1?0:dp[i-1][0]);
        }
        for(int i=1;i<m;i++){
    
    
            for(int j=1;j<n;j++){
    
    
                dp[i][j] = (obstacleGrid[i][j]==0?dp[i-1][j]+dp[i][j-1]:0);
            }
        }
        return dp[m-1][n-1];
    }
}

2 64. Minimum Path Sum

2.1 Subject understanding

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.
Input: integer array grid, representing a square of mxn, grid[i][j] representing the cost of passing the square.
Output: the minimum cost
that can go from the upper left corner to the lower right corner Rule: Only go down or right
Input: grid = [[1,3,1],[1,5,1],[4,2,1 ]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

2.2 Dynamic programming

class Solution {
    
    
    public int minPathSum(int[][] grid) {
    
    
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m][n];
        dp[0][0] = grid[0][0];
        for(int i=1;i<m;i++){
    
    
            dp[i][0] = dp[i-1][0]+grid[i][0];
        }
        
        for(int j=1;j<n;j++){
    
    
            dp[0][j] = dp[0][j-1]+grid[0][j];
        }
        
        for(int i=1;i<m;i++){
    
    
            for(int j=1;j<n;j++){
    
    
                dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];
            }
        }
        
        return dp[m-1][n-1];
    }
}

Guess you like

Origin blog.csdn.net/flying_all/article/details/111939332