LeetCode 62/63. Unique Paths 不同路径I/II(Java)

题目一:

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?

LeetCode 62/63. Unique Paths 不同路径I/II(Java)_第1张图片

Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1.Right -> Right -> Down
2.Right -> Down -> Right
3.Down -> Right -> Right

Example 2:
Input: m = 7, n = 3
Output: 28

Constraints:

  • 1 <= m, n <= 100
  • It’s guaranteed that the answer will be less than or equal to 2 * 10 ^ 9.

题目一解答:

拿到题目后,首先想到的是递归回溯的方法,但过于暴力耗时较多,所以这是一道动态规划的题。

我们用一个数组来存储每个格子上可到达的路径数量,因为只能往右走和往下走,所以能走到当前格子,一定是通过上面一个格子或者左边一个格子走到当前格子的,则每个格子上的数量=上面格子的数量+左边格子的数量,因此当前格子的路径数=从左边走过来的路径数+从上面走过来的路径数。

起始状态为:最上面一行和最左侧一行均为1
LeetCode 62/63. Unique Paths 不同路径I/II(Java)_第2张图片
依次向下走或向右走,当前状态只和左边和上边的格子有关
即可得状态方程为:
ways[i][j] = ways[i-1][j] + ways[i][j-1];
LeetCode 62/63. Unique Paths 不同路径I/II(Java)_第3张图片
代码实现如下:

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

题目二:

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.

Note: m and n will be at most 100.

Example 1:
Input:
[
[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和0。
但动态规划的整体思路和状态方程是没有变的,我们需要对网格中障碍物的地方做特殊处理:

假如一个6*4网格,红色0的位置即为有障碍物的位置,则每个网格的路径数如下:
LeetCode 62/63. Unique Paths 不同路径I/II(Java)_第4张图片

  1. 如果有障碍物的网格,则说明不能到达此地方,道路阻塞,则令该网格的状态数值为0,也就是说有障碍物的当前格子路径数为0。即if(obstacleGrid[i][j]==1) { ways[i][j]=0; }
  2. 接下来对初始状态初始化,即网格的左上角第一个格子的状态值为1,else if(i==0 && j==0) { ways[i][j]=1; }
  3. 对网格的第一行和第一列初始化状态,这里要注意,如果第一行中,有个网格有障碍物,则该网格后面的格子路径数也为0,向右走无法到达这些网格。第一列同理。因此对第一行做处理else if(i==0 && j>0) { ways[i][j] = ways[i][j-1]; }和对第一列做处理else if(j==0 && i>0) { ways[i][j] = ways[i-1][j]; }
  4. 其余网格则状态方程保持不变,即ways[i][j] = ways[i-1][j] + ways[i][j-1];

代码如下:

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int row = obstacleGrid.length;
        int column = obstacleGrid[0].length;
        int[][] ways = new int[row][column];
        for(int i=0; i<row; i++) {
            for(int j=0; j<column; j++) {
                if(obstacleGrid[i][j]==1) {
                    ways[i][j]=0;
                } else if(i==0 && j==0) {
                    ways[i][j]=1;
                } else if(i==0 && j>0) {
                    ways[i][j] = ways[i][j-1];
                } else if(j==0 && i>0) {
                    ways[i][j] = ways[i-1][j];
                } else {
                    ways[i][j] = ways[i-1][j] + ways[i][j-1];
                }
            }
        }
        return ways[row-1][column-1];
    }
}

你可能感兴趣的:(LeetCode)