Skip to content

Commit

Permalink
Merge pull request #248 from angie1015/patch-2
Browse files Browse the repository at this point in the history
Create 63.cpp
  • Loading branch information
larissalages authored Oct 18, 2020
2 parents 0d1301a + 9e2933e commit ffb7ec8
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions leetcode/cpp/dynamic programming/63.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//Unique Paths II

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid[0][0] == 1) {
return 0;
}
int n= obstacleGrid.size();
int m= obstacleGrid[0].size();
vector<vector<int> > dp(n,vector<int> (m, 0));
int i, j;
dp[0][0]=1;
for(i=1; i<n; i++){
if(obstacleGrid[i][0]==0)
dp[i][0]=dp[i-1][0];
}
for(i=1; i<m; i++){
if(obstacleGrid[0][i]==0)
dp[0][i]=dp[0][i-1];
}
for(i=1; i<n; i++){
for(j=1; j<m; j++){
if(obstacleGrid[i][j]==0){
dp[i][j]= dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[n-1][m-1];
}
};

0 comments on commit ffb7ec8

Please sign in to comment.