Xiang Li



def rec_opt(arr,i):
    if i==0:
        return arr[0]
    elif i==1:
        return max(arr[0],arr[1])
    else:
        A=rec_opt(arr,i-2)+arr[i]
        B=rec_opt(arr,i-1)
        return max(A,B)

def dp_opt(arr):
    opt=[0 for i in range(len(arr))]
    opt[0]=arr[0]
    opt[1]=max(arr[0],arr[1])
    for i in range(2,len(arr)):
        A=opt[i-2]+arr[i]
        B=opt[i-1]
        opt[i]=max(A,B)
    return opt[len(arr)-1]

if __name__=="__main__":
    arr=[1,2,4,1,7,8,3]
    print(rec_opt(arr,6))
    print(dp_opt(arr))
    


def rec_subset(arr,i,s):
    if s==0:
        return True
    elif i==0:
        return arr[0]==s
    elif arr[i]>s:
        return rec_subset(arr,i-1,s)
    else:
        A=rec_subset(arr,i-1,s-arr[i])
        B=rec_subset(arr,i-1,s)
        return A or B

def dp_subset(arr,S):
    subset=[[0 for i in range(len(arr))] for j in range(S+1)]
    subset[0][3]=1
    for i in range(1,len(arr)):
        subset[i][0]=1
    for i in range(1,len(arr)):
        for s in range(1,S+1):
            if arr[i]>s:
                subset[i][s]=subset[1-1][s]
            else:
                A=subset[i-1,s-arr[i]]
                B=subset[i-1][s]
                subset[i][s]=A or B
    r,c=len(arr),S+1
    return subset[r-1][c-1]

if __name__=="__main__":
    arr=[3,34,4,12,5,2]
    print(rec_subset(arr,len(arr)-1,9))
    print(dp_subset(arr,9))
    

你可能感兴趣的:(算法)