LeetCode - Unique Paths II
2013-12-21 03:28
330 查看
Unique Paths II
2013.12.21 03:19
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
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
Note: m and n will be at most 100.
Solution1:
This problem is a variation from "LeetCode - Unique Paths", only difference in that there're some grids defined as impenetrable obstacles.
I haven't come up with a solution using combinatorics. So I just did it the old-school way, dynamic programming with recurrence relation as:
a[x][y] = b[x][y] ? 0 : a[x - 1][y] + a[x][y - 1];
where a[x][y] means the number of unique paths to (x, y), b[x][y] = 1 means (x, y) is an obstacle, while 0 for free space.
Time complexity is O(m * n), space complexity is O(m * n).
Accepted code:
Solution2:
Space complexity can be optimized to linear, using 2 rows instead of m rows of extra space.
Time complexity is O(m * n), space complexity is O(n).
Accepted code:
2013.12.21 03:19
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
1and
0respectively 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.
Solution1:
This problem is a variation from "LeetCode - Unique Paths", only difference in that there're some grids defined as impenetrable obstacles.
I haven't come up with a solution using combinatorics. So I just did it the old-school way, dynamic programming with recurrence relation as:
a[x][y] = b[x][y] ? 0 : a[x - 1][y] + a[x][y - 1];
where a[x][y] means the number of unique paths to (x, y), b[x][y] = 1 means (x, y) is an obstacle, while 0 for free space.
Time complexity is O(m * n), space complexity is O(m * n).
Accepted code:
// 1WA, 1AC class Solution { public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int m, n; m = obstacleGrid.size(); if(m <= 0){ return 0; } n = obstacleGrid[0].size(); int **arr = nullptr; int i, j; arr = new int*[m]; for(i = 0; i < m; ++i){ arr[i] = new int ; } arr[0][0] = obstacleGrid[0][0] ? 0 : 1; for(i = 1; i < m; ++i){ // 1WA here, arr[i][0] = obstacleGrid[i][0] ? 0 : obstacleGrid[i - 1][0]; // arr[i][0] = obstacleGrid[i][0] ? 0 : arr[i - 1][0]; arr[i][0] = obstacleGrid[i][0] ? 0 : arr[i - 1][0]; } for(i = 1; i < n; ++i){ arr[0][i] = obstacleGrid[0][i] ? 0 : arr[0][i - 1]; } for(i = 1; i < m; ++i){ for(j = 1; j < n; ++j){ arr[i][j] = obstacleGrid[i][j] ? 0 : arr[i - 1][j] + arr[i][j - 1]; } } int res = arr[m - 1][n - 1]; for(i = 0; i < m; ++i){ delete[] arr[i]; } delete[] arr; return res; } };
Solution2:
Space complexity can be optimized to linear, using 2 rows instead of m rows of extra space.
Time complexity is O(m * n), space complexity is O(n).
Accepted code:
// 1AC, very nice class Solution { public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int m, n; m = obstacleGrid.size(); if(m <= 0){ return 0; } n = obstacleGrid[0].size(); int **arr = nullptr; int i, j; int flag = 0; arr = new int*[2]; for(i = 0; i < 2; ++i){ arr[i] = new int ; } flag = 0; arr[flag][0] = obstacleGrid[0][0] ? 0 : 1; for(i = 1; i < n; ++i){ arr[flag][i] = obstacleGrid[0][i] ? 0 : arr[flag][i - 1]; } for(i = 1; i < m; ++i){ flag = !flag; arr[flag][0] = obstacleGrid[i][0] ? 0 : arr[!flag][0]; for(j = 1; j < n; ++j){ arr[flag][j] = obstacleGrid[i][j] ? 0 : arr[!flag][j] + arr[flag][j - 1]; } } j = arr[flag][n - 1]; for(i = 0; i < 2; ++i){ delete[] arr[i]; } delete[] arr; return j; } };
相关文章推荐
- [LeetCode]--Unique Paths II
- [leetcode] Unique Paths II
- LeetCode---Unique Paths II
- LeetCode 63 - Unique Paths II
- LeetCode 063 Unique Paths II
- Unique Paths II leetcode java
- leetcode - Unique Paths II
- Unique Paths II 求在矩阵里从左上走到右下的总方法数(有障碍)@LeetCode
- [leetcode]Unique Paths II
- [leetcode]Unique Paths II @ Python
- leetcode:Unique Paths II
- LeetCode刷题【Array】 Unique Paths II
- [Leetcode] #62#63 Unique Paths I & II
- LeetCode-63-Unique Paths II(动规)-Medium
- [LeetCode]Unique Paths II
- [LeetCode] Unique Paths II
- 64 leetcode - Unique Paths II
- 【Leetcode】【Medium】Unique Paths II
- leetCode系列----Unique Paths II
- LeetCode——Unique Paths II