LeetCode 64. 最小路径和
原题链接
中等
作者:
nullwh
,
2020-10-11 21:14:49
,
所有人可见
,
阅读 319
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if (grid.empty()) return 0;
int n = grid.size(), m = grid[0].size();
vector<vector<int>> f(n, vector<int> (m, INT_MAX));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!i && !j) f[i][j] = grid[i][j];
else {
if (i) f[i][j] = min(f[i][j], f[i - 1][j] + grid[i][j]);
if (j) f[i][j] = min(f[i][j], f[i][j - 1] + grid[i][j]);
}
return f[n - 1][m - 1];
}
};