您的位置:首页 > 产品设计 > UI/UE

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
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
.

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: