Skip to content

Commit 9e2933e

Browse files
authored
Create 63.cpp
Added solution for Unique paths II
1 parent 7ef4ae8 commit 9e2933e

File tree

1 file changed

+31
-0
lines changed
  • leetcode/cpp/dynamic programming

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Unique Paths II
2+
3+
class Solution {
4+
public:
5+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
6+
if (obstacleGrid[0][0] == 1) {
7+
return 0;
8+
}
9+
int n= obstacleGrid.size();
10+
int m= obstacleGrid[0].size();
11+
vector<vector<int> > dp(n,vector<int> (m, 0));
12+
int i, j;
13+
dp[0][0]=1;
14+
for(i=1; i<n; i++){
15+
if(obstacleGrid[i][0]==0)
16+
dp[i][0]=dp[i-1][0];
17+
}
18+
for(i=1; i<m; i++){
19+
if(obstacleGrid[0][i]==0)
20+
dp[0][i]=dp[0][i-1];
21+
}
22+
for(i=1; i<n; i++){
23+
for(j=1; j<m; j++){
24+
if(obstacleGrid[i][j]==0){
25+
dp[i][j]= dp[i-1][j] + dp[i][j-1];
26+
}
27+
}
28+
}
29+
return dp[n-1][m-1];
30+
}
31+
};

0 commit comments

Comments
 (0)