1

Unique Paths

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?

动态规划

思路

state: dp[x][y]表示从起点到x,y的路径数
function: 每一步的路径相当于从它左边一步和它上边一步的加和
dp[x][y] = dp[x - 1][y] + dp[x][y - 1]
intialize: dp[0][0] = 1
// dp[0][i] = 1, dp[i][0] = 1//
二维数组里面第一行和第一列都为1 因为从起始点到此点的方法只有一种
answer: dp[n-1][m-1]

复杂度

时间是O(mn) 空间O(mn)

代码

public int uniquePaths(int m, int n) {
    int[][] dp = new int[m][n];
    dp[0][0] = 1;
    for (int i = 1; i < n; i++) {
        dp[0][i] = dp[0][i - 1];
    }
    for (int i = 1; i < m; i++) {
        dp[i][0] = dp[i - 1][0];
    }
    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(m*n) 空间 O(n)

代码

public int uniquePaths(int m, int n) {
    int[] dp = new int[n];
    dp[0] = 1;
    for (int i = 0; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[j] += dp[j - 1];
        }
    }
    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.

动态规划

思路

与1相同, 但是如果此处为1的话那么路径数应当为0

复杂度

时间是O(mn) 空间O(mn)

代码

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    int m = obstacleGrid.length;
    int n = obstacleGrid[0].length;
    int[][] dp = new int[m][n];
    for (int i = 0; i < m; i++) {
        if (obstacleGrid[i][0] != 1) {
            dp[i][0] = 1;
        } else {
            break;//后面所有的都无法到达所以break
        }
    }
    for (int j = 0; j < n; j++) {
        if (obstacleGrid[0][j] != 1) {
            dp[0][j] = 1;
        } else {
            break;
        }
    }
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (obstacleGrid[i][j] == 1) {
                dp[i][j] = 0;
            } else {
                dp[i][j] = dp[i-1][j] + dp[i][j - 1];
            }
        }
    }
    return dp[m - 1][n - 1];
}

一维动规

复杂度

时间 O(m*n) 空间O(n)

代码

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

lpy1990
26 声望10 粉丝