[Leetcode] Unique Paths 唯一路径

661 查看

Unique Paths I

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).

How many possible unique paths are there?

动态规划

复杂度

时间 O(NM) 空间 O(NM)

思路

因为要走最短路径,每一步只能向右方或者下方走。所以经过每一个格子路径数只可能源自左方或上方,这就得到了动态规划的递推式,我们用一个二维数组dp储存每个格子的路径数,则dp[i][j]=dp[i-1][j]+dp[i][j-1]。最左边和最上边的路径数都固定为1,代表一直沿着最边缘走的路径。

代码

public class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for(int i = 0; i < m; i++){
            dp[i][0] = 1;
        }
        for(int i = 0; i < n; i++){
            dp[0][i] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}

一维数组

复杂度

时间 O(NM) 空间 O(N)

思路

实际上我们可以复用每一行的数组来节省空间,每个元素更新前的值都是其在二维数组中对应列的上一行的值。这里dp[i] = dp[i - 1] + dp[i];

代码

public class Solution {
    public int uniquePaths(int m, int n) {
        int[] dp = new int[n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                // 每一行的第一个数都是1
                dp[j] = j == 0 ? 1 : dp[j - 1] + dp[j];
            }
        }
        return dp[n - 1];
    }
}

Unique Paths II

Follow up for "Unique Paths":

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

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

For example, There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

Note: m and n will be at most 100.

动态规划

复杂度

时间 O(NM) 空间 O(NM)

思路

解法和上题一模一样,只是要判断下当前格子是不是障碍,如果是障碍则经过的路径数为0。需要注意的是,最上面一行和最左边一列,一旦遇到障碍就不再赋1了,因为沿着边走的那条路径被封死了。

代码

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

一维数组

复杂度

时间 O(NM) 空间 O(N)

思路

和I中的空间优化方法一样,不过这里判断是否是障碍物,而且每行第一个点的值取决于上一行第一个点的值。

代码

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