-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_paths_II.cpp
25 lines (24 loc) · 985 Bytes
/
unique_paths_II.cpp
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
// Question Link - https://leetcode.com/problems/unique-paths-ii/description/?envType=study-plan-v2&envId=dynamic-programming
// solution
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
vector<vector<long long>> dp(obstacleGrid.size(), vector<long long> (obstacleGrid[0].size(), 0));
long long n = obstacleGrid.size();
long long m = obstacleGrid[0].size();
for(long long i=n-1; i>=0; i--)
{
for(long long j=m-1; j>=0; j--)
{
if(i==n-1 && j==m-1 && obstacleGrid[i][j] == 0) dp[i][j] = 1;
else if(i == n-1 && obstacleGrid[i][j] == 0) dp[i][j] += dp[i][j+1];
else if(j == m-1 && obstacleGrid[i][j] == 0) dp[i][j] += dp[i+1][j];
else if( obstacleGrid[i][j] == 0)
{
dp[i][j] += dp[i+1][j] + dp[i][j+1];
}
}
}
return dp[0][0];
}
};