-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnique_Path_II.cpp
More file actions
53 lines (45 loc) · 1.37 KB
/
Copy pathUnique_Path_II.cpp
File metadata and controls
53 lines (45 loc) · 1.37 KB
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
48
49
50
51
52
53
/*
Link:-https://leetcode.com/problems/unique-paths-ii/description/
*/
/* recursion*/
int func(int i,int j,vector<vector<int>>&dp,vector<vector<int>>& obstacleGrid)
{
if(i>=0 && j>=0 && obstacleGrid[i][j]==1 ) return 0;
if(i==0 && j==0) return 1;
if(i<0 || j<0) return 0;
if(dp[i][j]!=-1) return dp[i][j];
int up=func(i-1,j,dp,obstacleGrid);
int left=func(i,j-1,dp,obstacleGrid);
return dp[i][j]=(up+left)%1000000009;
}
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[0].size();
vector<vector<int>>dp(n+1,vector<int>(m+1,-1));
return func(n-1,m-1,dp,obstacleGrid)%1000000009;
}
/* dp */
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[0].size();
int dp[m][n];
///func(m-1,n-1,dp);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i>=0 && j>=0 && obstacleGrid[i][j]==1) dp[i][j]= 0;
else if(i==0 &&j==0) dp[i][j]=1;
else
{
int up=0;
int left=0;
if(i>0) up=dp[i-1][j];
if(j>0) left=dp[i][j-1];
dp[i][j]= up+left;
}
}
}
return dp[m-1][n-1];
}
};