My 12lines and 12ms DP solution in C++.


  • 0
    D

    class Solution {
    public:

    int minPathSum(vector<vector<int>>& grid) {
        int row = grid.size(),column = grid[0].size();
        vector<int> dp(column,INT_MAX);
        dp[0] = 0;
        for (int i = 0;i < row;++i)
            for (int j = 0;j < column;++j)
                dp[j] = j==0?dp[j]+grid[i][j]:grid[i][j] + min(dp[j],dp[j-1]);
        return dp[column-1];
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.