forked from starrohan999/Hacktoberfest-Accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnique Paths II
47 lines (43 loc) · 1.09 KB
/
Unique Paths II
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int rows = obstacleGrid.size();
int cols = obstacleGrid[0].size();
int dp[rows][cols];
bool flag = false;
//Fill 1st row
for(int i=0;i<cols;++i)
{
if(flag || obstacleGrid[0][i]==1)
{
flag = true;
dp[0][i] = 0;
}
else
dp[0][i] = 1;
}
//Fill 1st column
flag = false;
for(int i=0;i<rows;++i)
{
if(flag || obstacleGrid[i][0]==1)
{
flag = true;
dp[i][0] = 0;
}
else
dp[i][0] = 1;
}
for(int i=1;i<rows;++i)
{
for(int j=1;j<cols;++j)
{
if(obstacleGrid[i][j]==1)
dp[i][j] = 0;
else
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[rows-1][cols-1];
}
};