-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0063.cpp
59 lines (53 loc) · 1.34 KB
/
0063.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
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid) {
// return func1(obstacleGrid, 0, 0);
return func2(obstacleGrid);
}
// ** recursive, TLE
int func1(vector<vector<int>> &grid, int r, int c) {
if (r >= grid.size()) {
return 0;
}
if (c >= grid[r].size()) {
return 0;
}
if (grid[r][c] == 1) {
return 0;
}
if (r == grid.size() - 1 && c == grid[r].size() - 1) {
return 1;
}
return func1(grid, r + 1, c) + func1(grid, r, c + 1);
}
// ** recursive + memoization
int func2(vector<vector<int>> &grid) {
int row = grid.size();
if (row <= 0) {
return 0;
}
int col = grid[0].size();
if (col <= 0) {
return 0;
}
vector<vector<int>> memo(row, vector<int>(col, -1));
return helper2(grid, 0, 0, row, col, memo);
}
int helper2(vector<vector<int>> &grid, int r, int c, int row, int col,
vector<vector<int>> &memo) {
if (r >= row)
return 0;
if (c >= col)
return 0;
if (grid[r][c] == 1)
return 0;
if (r == row - 1 && c == col - 1)
return 1;
if (memo[r][c] >= 0)
return memo[r][c];
memo[r][c] = 0;
memo[r][c] += helper2(grid, r + 1, c, row, col, memo);
memo[r][c] += helper2(grid, r, c + 1, row, col, memo);
return memo[r][c];
}
};