``````class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();

vector<vector<int>> sum(m,vector<int>(n,grid[0][0]));

for(int i = 1;i < m;i++)
sum[i][0] = sum[i-1][0] + grid[i][0];

for(int j = 1;j < n;j++)
sum[0][j] = sum[0][j - 1] + grid[0][j];

for(int i = 1;i < m;i++)
for(int j = 1;j < n;j++)
sum[i][j] = min(sum[i-1][j],sum[i][j-1]) + grid[i][j];

return sum[m-1][n-1];
}
};``````

``````Runtime: 20 ms, faster than 59.19% of C++ online submissions for Minimum Path Sum.
Memory Usage: 10.5 MB, less than 9.39% of C++ online submissions for Minimum Path Sum.``````

``````class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<int> cur(m, grid[0][0]);
for (int i = 1; i < m; i++)
cur[i] = cur[i - 1] + grid[i][0];

for (int j = 1; j < n; j++) {
cur[0] += grid[0][j];
for (int i = 1; i < m; i++)
cur[i] = min(cur[i - 1], cur[i]) + grid[i][j];
}
return cur[m - 1];
}
};``````

``````Runtime: 16 ms, faster than 91.86% of C++ online submissions for Minimum Path Sum.
Memory Usage: 10.2 MB, less than 9.39% of C++ online submissions for Minimum Path Sum.``````

``````#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <list>
using namespace std;

int minPathSum(vector<vector<int>>& grid)
{
int m = grid.size();
int n = grid[0].size();
vector<int> cur(m, grid[0][0]);
for (int i = 1; i < m; i++)
cur[i] = cur[i - 1] + grid[i][0];

for (int j = 1; j < n; j++) {
cur[0] += grid[0][j];
for (int i = 1; i < m; i++)
cur[i] = min(cur[i - 1], cur[i]) + grid[i][j];
}
return cur[m - 1];
}

int main()
{
vector<vector<int>> path{{1,3,1},{1,5,1},{4,2,1}};
cout << minPathSum(path);
return 0;
}``````