forked from sajdakabir/dp-dynamic-programming-series
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnique_paths_II.cpp
More file actions
28 lines (27 loc) · 865 Bytes
/
Unique_paths_II.cpp
File metadata and controls
28 lines (27 loc) · 865 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
25
26
27
28
// memoization
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[0].size();
vector<vector<int>> dp(m,vector<int>(n,-1));
if(obstacleGrid[m-1][n-1]==1)return 0;
for(int j=n-1;j>=0;j--){
for(int i=m-1;i>=0;i--){
if(i==m-1 && j==n-1){
dp[i][j]=1;
continue;
}
if(obstacleGrid[i][j]==1 && i<m-1 && j<n-1) {
dp[i][j]=0;
}
int right=0;
if(j<n-1){
right+=dp[i][j+1];
}
int down=0;
if(i<m-1){
down+=dp[i+1][j];
}
}
}
return dp[0][0];
}