forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnique_Paths_II.cpp
More file actions
21 lines (19 loc) · 822 Bytes
/
Unique_Paths_II.cpp
File metadata and controls
21 lines (19 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m = obstacleGrid.size();
if(m == 0) return 0;
int n = obstacleGrid[0].size();
if(n == 0) return 0;
vector <vector<int> > dp(m + 1, vector<int>(n + 1, 0));
dp[0][0] = (obstacleGrid[0][0] == 1) ? 0 : 1;
for(int i = 1; i < m; ++i) dp[i][0] = (dp[i - 1][0] == 0 or obstacleGrid[i][0] == 1) ? 0 : 1;
for(int i = 1; i < n; ++i) dp[0][i] = (dp[0][i - 1] == 0 or obstacleGrid[0][i] == 1) ? 0 : 1;
for(int i = 1; i < m; ++i) {
for(int j = 1; j < n; ++j) {
dp[i][j] = (obstacleGrid[i][j] == 1) ? 0 : dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
};