[leetcode] Minimum Path Sum

Minimum Path Sum

dp入门题,状态fun[i][j]=min(fun[i-1][j],fun[i][j-1])+grid[i][j];

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {//
        int row=grid.size();//行数
        if (row==0) {
            return 0;
        }
        int column=grid[0].size();//列数
        
        vector<vector<int>> fun(row,vector<int>(column));//开辟相同大小的矩阵
        //处理边界
        fun[0][0]=grid[0][0];
        int i,j;
        for (i=1; i<row; ++i) {//第一列
            fun[i][0]=fun[i-1][0]+grid[i][0];
        }
        for (j=1; j<column; ++j) {//第一行
            fun[0][j]=fun[0][j-1]+grid[0][j];
        }
        
        for (i=1; i<row; ++i) {
            for (j=1; j<column; ++j) {
                fun[i][j]=min(fun[i-1][j],fun[i][j-1])+grid[i][j];//min()为库函数
            }
        }
        return fun[row-1][column-1];
    }
};


class Solution {
public:
    //状态定义为从起点--当前节点的最小路径和
    int minPathSum(vector<vector<int> > &grid) {
        if(grid.size()==0){
            return 0;
        }
        int sum[grid.size()][grid[0].size()];
        //状态初始化
        sum[0][0]=grid[0][0];
        for(int i=1;i<grid.size();++i){
            sum[i][0]=grid[i][0]+sum[i-1][0];
        }
        for(int j=1;j<grid[0].size();++j){
            sum[0][j]=grid[0][j]+sum[0][j-1];
        }
        //状态转移
        for(int i=1;i<grid.size();++i){//行
            for(int j=1;j<grid[0].size();++j){//列
                sum[i][j]=grid[i][j]+min(sum[i][j-1],sum[i-1][j]);
            }
        }
        return sum[grid.size()-1][grid[0].size()-1];
    }
};


class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m=grid.size();//row
        int n=grid[0].size();//column

        vector<vector<int> > dp(m,vector<int>(n));//二维
        for(int i=0;i<m;++i){
            for(int j=0;j<n;++j){
                if(i==0){
                    if(j==0){
                        dp[i][j]=grid[i][j];
                    }else{
                        dp[i][j]=grid[i][j]+dp[i][j-1];
                    }
                }else if(j==0){
                    dp[i][j]=grid[i][j]+dp[i-1][j];
                }else{
                    dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];
                }
            }
        }
        return dp[m-1][n-1];
    }
};

优化空间

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m=grid.size();//row
        int n=grid[0].size();//column

        vector<int> dp(n);//一维
        for(int i=0;i<m;++i){
            for(int j=0;j<n;++j){
                if(i==0){
                    if(j==0){
                        dp[j]=grid[i][j];
                    }else{
                        dp[j]=grid[i][j]+dp[j-1];
                    }
                }else if(j==0){
                    dp[j]=grid[i][j]+dp[j];
                }else{
                    dp[j]=min(dp[j],dp[j-1])+grid[i][j];
                }
            }
        }
        return dp[n-1];
    }
};


参考: Minimum path sum

你可能感兴趣的:([leetcode] Minimum Path Sum)