forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique-paths-ii.cpp
31 lines (26 loc) · 912 Bytes
/
unique-paths-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Time: O(m * n)
// Space: O(min(m, n))
class Solution {
public:
/**
* @param obstacleGrid: A list of lists of integers
* @return: An integer
*/
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid) {
const int m = obstacleGrid.size();
const int n = obstacleGrid[0].size();
vector<vector<int>> path(2, vector<int>(n, 0));
path[0][0] = obstacleGrid[0][0] == 0? 1 : 0;
for (int j = 1; j < n; ++j) {
path[0][j] = obstacleGrid[0][j] == 0? path[0][j - 1] : 0;
}
for (int i = 1; i < m; ++i) {
path[i % 2][0] = obstacleGrid[i][0] == 0? path[(i - 1) % 2][0] : 0;
for (int j = 1; j < n; ++j) {
path[i % 2][j] = obstacleGrid[i][j] == 0?
path[(i - 1) % 2][j] + path[i % 2][j - 1] : 0;
}
}
return path[(m - 1) % 2][n - 1];
}
};