算法(六) 动态规划

1、背包承重

def splitPack(n):
    step = [1,2,3]
    dp = [0] * (n + 1)
    dp[0] = 1
    for i in step:
        for j in range(i,n + 1):
            dp[j] += dp[j - i]
    return dp[n]

2、背包承重带有属性

def splitPack(n):
    things_num = [3,5,1,2,3]
    things_price = [300,500,100,200,300]
    dp = [0] * (n + 1)
    for i in range(n + 1):
        if i >= things_num[0]:
            dp[i] = things_price[0]
    res = copy.deepcopy(dp)
    for index,i in enumerate(things_num):
        for j in range(i,n + 1):
            t = 0 if j - i < 0 else j - i
            dp[j] = max(res[j],res[t] + things_price[index])
        res = copy.deepcopy(dp)
    return dp[n]

3、数组内连续最大值

def computeMaxSum():
    data_list = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
    dp = [0] * len(data_list)
    dp[0] = data_list[0]
    for i in range(1,len(data_list)):
        dp[i] = max(data_list[i],dp[i - 1] + data_list[i])
    return max(dp)
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页