leetcode64. Minimum Path Sum
阿新 • • 發佈:2018-08-31
turn ++ pre ini res nim leetcode class i++
DP
class Solution { public: int minPathSum(vector<vector<int>>& grid) { int height = grid.size(); int width = grid[0].size(); vector<vector<int>> result(height,vector<int>(width)); result[0][0] = grid[0][0]; for(int i = 0;i < height;i++){for(int j = 0;j < width;j++){ if(i != 0 && j != 0) result[i][j] = min(result[i][j-1],result[i-1][j]) + grid[i][j]; else if(i == 0) result[i][j] = result[i][j-1] + grid[i][j]; else result[i][j]= result[i-1][j] + grid[i][j]; } } return result[height-1][width-1]; } };
leetcode64. Minimum Path Sum