14- cut the rope

Title: Here n is a length of rope, the rope please cut segment m (m, n are integers and are larger than 1), the length of each rope is referred to as k [0], k [1], ... k [m]. Will k [0] * k [1] * k [2] * ... * k [m] maximum possible score is how much?

def max_product_cut(n):
    if n<2:
        return 0
    if n == 2:
        return 1
    if n == 3:
        return 2
    i = 4
    j = 1
    products = [0 for i in range(n+1)]
    products[1] = 1
    products[2] = 2
    products[3] = 3

    while i<=n:
        max = 0
        j=2
        while j<=i//2:
            res = products[j]*products[i-j]
            if max<res:
                max = res
            j+=1
        products[i] = max

        i+=1

    return products[-1]

Note:

Using dynamic programming, products [i] storing the maximum length of the rope of product i. Local clever in that the maximum product of the whole string of n, is obtained as long as the maximum product of n / 2 of the can, the rest in the first half of the array has been calculated is stored. Equation of state f (n) = max (f (i) * f (ni))

Guess you like

Origin www.cnblogs.com/kingshine007/p/11348693.html