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