forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0064-minimum-path-sum.cpp
29 lines (28 loc) · 1.24 KB
/
0064-minimum-path-sum.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
class Solution{
public:
void Helper(vector<vector<int>> & grid, vector<vector<int>> & dp, int i, int k){
int X = grid.size();
int Y = grid[0].size();
for(int i = 0; i < X; i++){
for(k = 0; k < Y; k++){
if((i - 1 >= 0) && (k - 1 >= 0)){
dp[i][k] = grid[i][k] + min(dp[i - 1][k], dp[i][k - 1]);
}
else{
if(i - 1 >= 0){
dp[i][k] = grid[i][k] + dp[i - 1][k];
}
if(k - 1 >= 0){
dp[i][k] = grid[i][k] + dp[i][k - 1];
}
}
}
}
}
int minPathSum(vector<vector<int>>& grid){
vector<vector<int>> dp(grid.size(), vector<int>(grid[0].size()));
dp[0][0] = grid[0][0];
Helper(grid, dp, grid.size() - 1, grid[0].size() - 1);
return dp[grid.size() - 1][grid[0].size() - 1];
}
};