LeetCode 64

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int n=grid.size();
        int m=grid[0].size();
        const int N=210;
        int dp[N][N];
        memset(dp,0x3f,sizeof dp);
        dp[0][0]=grid[0][0];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(i==0&&j==0)
                continue;
                if(i-1>=0&&i-1<n)
                dp[i][j]=min(dp[i-1][j],dp[i][j]);
                if(j-1>=0&&j-1<m)
                dp[i][j]=min(dp[i][j-1],dp[i][j]); 
                dp[i][j]+=grid[i][j];
            }
        }
        return dp[n-1][m-1];
    }
};

你可能感兴趣的:(leetcode,算法,动态规划)