Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

最小路径和 #423

Open
Pcjmy opened this issue Jan 26, 2023 · 1 comment
Open

最小路径和 #423

Pcjmy opened this issue Jan 26, 2023 · 1 comment

Comments

@Pcjmy
Copy link
Contributor

Pcjmy commented Jan 26, 2023

No description provided.

@lxy-Jason
Copy link
Contributor

/**
 * @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]
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants