LintCode:背包问题 II

来源:互联网 发布:金融行业 数据安全 编辑:程序博客网 时间:2024/05/16 06:00

LintCode:背包问题 II

class Solution:    # @param m: An integer m denotes the size of a backpack    # @param A & V: Given n items with size A[i] and value V[i]    # @return: The maximum value    def backPackII(self, m, A, V):        # write your code here        a = m        b = len(A)        A.insert(0, 0)        V.insert(0, 0)        ans = [[0 for i in range(a+1)] for j in range(b+1)]        for i in range(1, b+1):            for j in range(1, a+1):                if j >= A[i]:                    ans[i][j] = max(ans[i-1][j-A[i]] + V[i], ans[i-1][j])                else:                    ans[i][j] = max(ans[i-1][j], ans[i][j-1])        return ans[b][a]
0 0