题解 | #剪绳子#

剪绳子

https://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8

js实现

function cutRope(number)
{
    // if(number===2) return 1
    // if(number===3) return 2
    // let max = 0;
    // for(let i = 2,j = number-2;i<=j;i++,j--) {
    //     max = Math.max(max, Math.max(cutRope(i), i)* Math.max(cutRope(j), j))
    // }
    // console.log(max)
    // return max
    let dp = []
    dp[2] = 1
    dp[3] = 2
    
    return getChengji(number, dp)
}
function getChengji(number, dp) {
    if(dp[number]) return dp[number]
    let max = 0;
    for(let i = 2,j = number-2;i<=j;i++,j--) {
        max = Math.max(max, Math.max(getChengji(i, dp), i)* Math.max(getChengji(j, dp), j))
    }
    console.log(max)
    dp[number] = max
    return max
}
module.exports = {
    cutRope : cutRope
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务