Sunny-117/js-challenges

最小路径和

Pcjmy opened this issue · 1 comments

Pcjmy commented
最小路径和
/**
 * @param {number[][]} dp
 * @return {number}
 */
var minPathSum = function(dp) {
    let row = dp[0].length;
    let col = dp.length

    for(let i = 1; i < row; i++){ //第一排初始化
        dp[0][i] += dp[0][i - 1]
    }
    for(let i = 1; i < col; i++){ //第一列初始化
        dp[i][0] += dp[i - 1][0]
    }
    for(let i = 1; i < col; i++){
        for(let j = 1; j < row; j++){
            dp[i][j] += Math.min(dp[i][j - 1],dp[i - 1][j]) //注意这里是+= 左边或者上边小的那一个
        }
    }
    return dp[col - 1][row - 1]
};