# LC 63. Unique Paths II ### [Problem link](https://leetcode.com/problems/unique-paths-ii/) ###### tags: `leedcode` `python` `c++` `medium` `DP` You are given an <code>m x n</code> integer array <code>grid</code>. There is a robot initially located at the **top-left corner** (i.e., <code>grid[0][0]</code>). The robot tries to move to the **bottom-right corner** (i.e., <code>grid[m - 1][n - 1]</code>). The robot can only move either down or right at any point in time. An obstacle and space are marked as <code>1</code> or <code>0</code> respectively in <code>grid</code>. 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 <code>2 * 10<sup>9</sup></code>. **Example 1:** <img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg" style="width: 242px; height: 242px;" /> ``` 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:** <img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg" style="width: 162px; height: 162px;" /> ``` Input: obstacleGrid = [[0,1],[0,0]] Output: 1 ``` **Constraints:** - <code>m == obstacleGrid.length</code> - <code>n == obstacleGrid[i].length</code> - <code>1 <= m, n <= 100</code> - <code>obstacleGrid[i][j]</code> is <code>0</code> or <code>1</code>. ## Solution 1 - DP #### Python ```python= class Solution: def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: m = len(obstacleGrid) n = len(obstacleGrid[0]) dp = [[0 for _ in range(n)] for _ in range(m)] if obstacleGrid[0][0] == 1: return 0 for i in range(m): if obstacleGrid[i][0] == 1: break dp[i][0] = 1 for i in range(n): if obstacleGrid[0][i] == 1: break dp[0][i] = 1 for i in range(1, m): for j in range(1, n): if obstacleGrid[i][j] == 0: dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[-1][-1] ``` #### C++ ```cpp= class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<vector<int>> dp(m, vector<int>(n, 0)); 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++) { if (obstacleGrid[i][j] == 1) { continue; } dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1]; } }; ``` ## Solution 2 - DP #### Python ```python= class Solution: def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: m = len(obstacleGrid) n = len(obstacleGrid[0]) dp = [0] * n for i in range(n): if obstacleGrid[0][i] == 1: break dp[i] = 1 for i in range(1, m): for j in range(n): if obstacleGrid[i][j] == 1: dp[j] = 0 elif j > 0: dp[j] += dp[j - 1] return dp[-1] ``` #### C++ ```cpp= class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<int> dp(n, 0); for (int i = 0; i < n; i++) { if (obstacleGrid[0][i] == 1) { break; } dp[i] = 1; } for (int i = 1; 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]; } }; ``` >### Complexity >m = obstacleGrid.length >n = obstacleGrid[i].length >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(mn) | O(mn) | >| Solution 2 | O(mn) | O(n) | ## Note solutions 1: 2 dimensional dp solutions 2: 1 dimensional dp