Skip to content

Instantly share code, notes, and snippets.

@peter-jung
Created February 23, 2017 07:03
Show Gist options
  • Save peter-jung/10a79192cdd8439dcd28a3cde9512e18 to your computer and use it in GitHub Desktop.
Save peter-jung/10a79192cdd8439dcd28a3cde9512e18 to your computer and use it in GitHub Desktop.
'''
https://en.wikipedia.org/wiki/Knapsack_problem
'''
v = [1,2,3,2,10]
w = [2,5,7,8,15]
W = 22
n = len(v)
m = [[0 for _ in range(W+1)] for _ in range(n)]
for i in range(n):
for j in range(W+1):
if w[i] <= j:
m[i][j] = max(m[i-1][j], m[i-1][j-w[i]]+v[i])
else:
m[i][j] = m[i-1][j]
for x in m:
print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment