-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_UniquePathII.cpp
174 lines (159 loc) · 4.54 KB
/
10_UniquePathII.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
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// https://leetcode.com/problems/unique-paths-ii/
// You are given an m x n integer array grid. There is a robot initially located
// at the top-left corner (i.e., grid[0][0]). The robot tries to move to the
// bottom-right corner (i.e., grid[m-1][n-1]). The robot can only move either
// down or right at any point in time.
// An obstacle and space are marked as 1 or 0 respectively in grid. A path that
// the robot takes cannot include any square that is an obstacle.
// Return the number of possible unique paths that the robot can take to reach
// the bottom-right corner.
// The testcases are generated so that the answer will be less than or equal
// to 2 * 109.
#include <bits/stdc++.h>
using namespace std;
class Solution4
{
// Tabulation : Space Optimisation // Incomplete
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
{
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<int> dp(n);
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
int left = 0 ;
if (obstacleGrid[i][j]){
dp[j] = 0;
}
else {
dp[j] = left + dp[j];
}
}
}
}
};
class Solution3
{
// Tabulation
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
{
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<vector<int>> dp(m, vector<int>(n));
dp[0][0] = 1;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (obstacleGrid[i][j])
{
dp[i][j] = 0;
}
else
{
if (i == 0)
{
if (j > 0)
dp[i][j] = dp[i][j - 1];
}
else if (j == 0 && i > 0)
{
dp[i][j] = dp[i - 1][j];
}
else
{
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
}
return dp[m - 1][n - 1];
}
};
class Solution2
{
// Recursion : Memoization
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
{
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<vector<int>> dp(m, vector<int>(n, -1));
// return solve(obstacleGrid, dp);
return dp[m - 1][n - 1] = solve(obstacleGrid, dp);
}
int solve(vector<vector<int>> &obs, vector<vector<int>> &dp, int i = 0, int j = 0)
{
if (i == obs.size() - 1 && j == obs[0].size() - 1 && !obs[i][j])
{
return 1;
}
if (i >= obs.size() || j >= obs[0].size() || obs[i][j])
{
return 0;
}
if (dp[i][j] != -1)
return dp[i][j];
// Go down -> Go right
return dp[i][j] = solve(obs, dp, i + 1, j) + solve(obs, dp, i, j + 1);
}
};
class Solution1
{
// Recursion Improved
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
{
return solve(obstacleGrid);
}
int solve(vector<vector<int>> &obs, int i = 0, int j = 0)
{
if (i == obs.size() - 1 && j == obs[0].size() - 1 && !obs[i][j])
{
return 1;
}
if (i >= obs.size() || j >= obs[0].size() || obs[i][j])
{
return 0;
}
// Go down -> Go right
return solve(obs, i + 1, j) + solve(obs, i, j + 1);
}
};
class Solution
{
// Simple Recursion
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
{
int ans = 0;
solve(obstacleGrid, ans);
return ans;
}
void solve(vector<vector<int>> &obs, int &ans, int i = 0, int j = 0)
{
if (i == obs.size() - 1 && j == obs[0].size() - 1)
{
++ans;
return;
}
if (i >= obs.size() || j >= obs[0].size() || obs[i][j])
{
return;
}
// Go down
solve(obs, ans, i + 1, j);
// Go right
solve(obs, ans, i, j + 1);
}
};
int main()
{
vector<vector<int>> grid = {{0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}};
Solution3 Obj1;
cout << Obj1.uniquePathsWithObstacles(grid);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}