Minimum Path Sum

 

CREATED TIME: Feb 15, 2020 1:09 AM UPDATED TIME: Feb 15, 2020 1:09 AM 난이도: Medium

class Solution { public: int minPathSum(vector<vector>& grid) { int x = grid.size(); int y = grid[0].size(); vector<vector> v(x, vector(y, 0)); v[0][0] = grid[0][0];

        for(int i=1; i<y; i++){
            v[0][i] = grid[0][i] + v[0][i-1];
        }
        
        for(int i=1; i<x; i++){
            for(int j=0; j<y; j++){
                if (j == 0){
                    v[i][j] = v[i-1][j];
                }else{
                    v[i][j] = min({
                        v[i-1][j],
                        v[i][j-1],
                    });
                }
                v[i][j] += grid[i][j];
            }
        }
        return v[x-1][y-1];
    }
};