1. 程式人生 > >leetcode64. Minimum Path Sum

leetcode64. Minimum Path Sum

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