[LeetCode] 63. Unique Paths II

You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.

Return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The testcases are generated so that the answer will be less than or equal to 2 * 109.

Example 1:

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

Example 2:

Input: obstacleGrid = [[0,1],[0,0]]
Output: 1

Constraints:

  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j] is 0 or 1.

不同路径 II。

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

网格中的障碍物和空位置分别用 1 和 0 来表示。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-paths-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意跟版本一一样,唯一的不同点在于路径上会有障碍物。依然是返回到底有多少不同的路径能从左上角走到右下角。我还是提供自上而下和自下而上两种解法。

DP自上而下

时间O(mn)

空间O(mn)

Java实现

 1 class Solution {
 2     int[][] memo;
 3     
 4     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
 5         int m = obstacleGrid.length;
 6         int n = obstacleGrid[0].length;
 7         memo = new int[m][n];
 8         return dp(obstacleGrid, m - 1, n - 1);
 9     }
10     
11     private int dp(int[][] grid, int i, int j) {
12         // base case
13         // 越界或者遇到障碍物,直接返回0
14         if (i < 0 || j < 0 || grid[i][j] == 1) {
15             return 0;
16         }
17         if (i == 0 && j == 0) {
18             return 1;
19         }
20         if (memo[i][j] > 0) {
21             return memo[i][j];
22         }
23         memo[i][j] = dp(grid, i - 1, j) + dp(grid, i, j - 1);
24         return memo[i][j];
25     }
26 }
27 
28 // dp自上而下

 

DP自下而上

时间O(mn)

空间O(mn)

Java实现

 1 class Solution {
 2     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
 3         int m = obstacleGrid.length;
 4         int n = obstacleGrid[0].length;
 5         int[][] dp = new int[m][n];
 6         for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
 7             dp[i][0] = 1;
 8         }
 9         for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
10             dp[0][j] = 1;
11         }
12         
13         for (int i = 1; i < m; i++) {
14             for (int j = 1; j < n; j++) {
15                 if (obstacleGrid[i][j] == 0) {
16                     dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
17                 }
18             }
19         }
20         return dp[m - 1][n - 1];
21     }
22 }
23 
24 // dp自下而上

 

相关题目

62. Unique Paths

63. Unique Paths II

64. Minimum Path Sum

LeetCode 题目总结

posted @ 2020-03-23 12:25  CNoodle  阅读(433)  评论(0编辑  收藏  举报