menu_book Course Curriculum (Data Structures & Algorithms) expand_more
Complexity & Foundations
Arrays & Strings
Linked Lists
Stacks & Queues
Hashing
Trees & Heaps
Graphs
0 1 knapsack
Learn 0 1 knapsack in Data Structures & Algorithms with a focused explanation, a topic-specific example, edge guidance, and a mastery check.
12 min read
•
Advanced
Advertisement
data-structures-algorithms-079.py
python
weights=[2,3,4];values=[4,5,7];capacity=5
dp=[0]*(capacity+1)
for w,v in zip(weights,values):
for c in range(capacity,w-1,-1):dp[c]=max(dp[c],dp[c-w]+v)
print(dp[capacity])
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content