class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if (grid.empty()) return 0;
int res[1000];
int rowCount = grid.size();
int colCount = grid[0].size();
res[0] = grid[0][0];
for (int i = 1; i < colCount; ++i) {
res[i] = grid[0][i]+res[i-1];
}
for (int i = 1; i < rowCount; ++i) {
for (int j = 0; j < colCount; ++j) {
if (j == 0) res[j]+= grid[i][j];
else {
res[j] = res[j] > res[j - 1] ? res[j - 1] : res[j];
res[j] += grid[i][j];
}
}
}
return res[colCount - 1];
}
};