To prove safety offer_ dynamic programming _ cut the rope

Dynamic programming _ cut the rope

Here Insert Picture Description
Ideas: Dynamic Programming

class Solution():
    def maxProfuctAfterCutting(self,length):
        if length < 2:
            return 0
        if length == 2:
            return 1
        if length == 3:
            return 2
        products = [0,1,2,3]
        for i in range(4,length+1):
            max = 0
            for j in range(1,i//2+1):
        # 思路:每次求解值时将其他小于需要求解的长度是都列出来放在一个数组里
        # 如:求长度为5,最优解数组里必须得有长度为1,2,3,4的最优解值
        # 注:此处使用列表保存最优解数组是为了性能优化,虽然递归求解也能解出,但会造成大量重复执行
                temp = products[j] * products[i-j]
                if temp>max:
                    max = temp
                products.append(max)
        return products[length]

Reprinted: https: //www.cnblogs.com/tianqizhi/p/9642877.html

Published 31 original articles · won praise 0 · Views 740

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105011293