-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path64. Minimum Path Sum
68 lines (53 loc) · 1.61 KB
/
64. Minimum Path Sum
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
Recursive Solution
int getMinPath(vector <vector <int>> &grid, int i, int j) {
if (i == 0 && j == 0) {
return grid[i][j];
}
int m = INT_MAX;
if (j > 0) {
m = grid[i][j] + getMinPath(grid, i, j - 1);
}
if (i > 0) {
int s = grid[i][j] + getMinPath(grid, i - 1, j);
m = min(m, s);
}
return m;
}
Top Down DP - Memoization
int getMinPath(vector <vector <int>> &grid, int i, int j, vector <vector<int>> dp) {
if (i == 0 && j == 0) {
return grid[i][j];
}
if (dp[i][j] != -1) {
return dp[i][j];
}
int m = INT_MAX;
if (j > 0) {
m = grid[i][j] + getMinPath(grid, i, j - 1, dp);
}
if (i > 0) {
int s = grid[i][j] + getMinPath(grid, i - 1, j, dp);
m = min(m, s);
}
dp[i][j] = m;
return dp[i][j];
}
Bottom Up DP - Table Filling
int getMinPath(vector <vector <int>> &grid) {
int r = grid.size();
int c = grid[0].size();
vector <vector <int>> dp(r, vector <int>(c, 0));
dp[0][0] = grid[0][0];
for (int i = 1; i < c; i++) {
dp[0][i] = grid[0][i] + dp[0][i - 1];
}
for (int i = 1; i < r; i++) {
dp[i][0] = grid[i][0] + dp[i - 1][0];
}
for (int i = 1; i < r; i++) {
for (int j = 1; j < c; j++) {
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[r - 1][c - 1];
}