Skip to content

Commit 1e8df97

Browse files
authoredOct 10, 2021
Create MinimumPathSum.cpp
1 parent a0efc6e commit 1e8df97

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Problem Link:
2+
//https://leetcode.com/problems/minimum-path-sum/
3+
4+
//Standard DP Approach by using memoization
5+
6+
class Solution {
7+
public:
8+
int minPathSum(vector<vector<int>>& grid) {
9+
int m = grid.size();
10+
int n = grid[0].size();
11+
vector<int> dp(m, grid[0][0]);
12+
for (int i = 1; i < m; i++)
13+
dp[i] = dp[i - 1] + grid[i][0];
14+
for (int j = 1; j < n; j++) {
15+
dp[0] += grid[0][j];
16+
for (int i = 1; i < m; i++)
17+
dp[i] = min(dp[i - 1], dp[i]) + grid[i][j];
18+
}
19+
return dp[m - 1];
20+
}
21+
};

0 commit comments

Comments
 (0)
Please sign in to comment.