Given the dynamic programming formula f[i][j]=min(f[i-1][j],f[i][j-1])+grid[i][j]
:
Assume that you are populating the table row by row, the current value (f[i][j]
) will be used immediately in the calculation of f[i][j+1]
, so there is no need to store all the previous column values.
Therefore, you can do it in linear space complexity. Below is a sample code courtesy of Linfuzki@.
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
if (grid.empty() || grid[0].empty()) return 0;
int m = grid.size(), n = grid[0].size();
vector<int> dp(n + 1, INT_MAX);
dp[1] = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
dp[j + 1] = min(dp[j + 1], dp[j]) + grid[i][j];
return dp.back();
}
};