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

三角形最小路径和 #1070

Open
pwstrick opened this issue Aug 4, 2020 · 0 comments
Open

三角形最小路径和 #1070

pwstrick opened this issue Aug 4, 2020 · 0 comments
Labels
Leetcode Leetcode的题目

Comments

@pwstrick
Copy link
Owner

pwstrick commented Aug 4, 2020

120. 三角形最小路径和

/**
 * @param {number[][]} triangle
 * @return {number}
 */
var minimumTotal = function(triangle) {
    const len = triangle.length;
    if(len == 0)
        return 0;
    let dp = [triangle[0]],     //初始化dp
        i, j;
    for(i=1; i<len; i++) {
        dp[i] = [dp[i-1][0]+triangle[i][0]];        //初始化第一列
        for(j=1, curLen=triangle[i].length; j<curLen; j++) {
            if(dp[i-1][j] === undefined) {
                dp[i][j] = dp[i-1][j-1] + triangle[i][j];       //特殊情况
            }else {
                dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j-1]) + triangle[i][j];     //状态转移方程
            }
            
        }
    }
    return Math.min.apply(this, dp[i-1]);
};
@pwstrick pwstrick added the Leetcode Leetcode的题目 label Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Leetcode Leetcode的题目
Projects
None yet
Development

No branches or pull requests

1 participant