📖 Scenario: You are helping a shop owner decide how to fill a knapsack with items to maximize value. The shop owner can take unlimited quantities of each item.Each item has a weight and a value. The knapsack has a maximum weight capacity.
🎯 Goal: Build a program that calculates the maximum total value the shop owner can carry in the knapsack using the unbounded knapsack approach.
📋 What You'll Learn
Create arrays for item weights and values with exact values
Create a variable for the knapsack capacity
Use a dynamic programming approach to solve the unbounded knapsack problem
Print the maximum value that fits in the knapsack
💡 Why This Matters
🌍 Real World
This problem models situations where you can use unlimited quantities of resources to maximize profit or value, such as packing goods, cutting rods, or resource allocation.
💼 Career
Understanding dynamic programming and knapsack problems is important for software engineers working on optimization, resource management, and algorithm design.
Progress0 / 4 steps
1
Create item weights and values arrays
Create an array called weights with these exact values: 1, 3, 4, 5. Also create an array called values with these exact values: 10, 40, 50, 70.
DSA Javascript
Hint
Use const weights = [1, 3, 4, 5]; and const values = [10, 40, 50, 70];
2
Set the knapsack capacity
Create a variable called capacity and set it to 8.
DSA Javascript
Hint
Use const capacity = 8; to set the knapsack capacity.
3
Calculate maximum value using unbounded knapsack logic
Create an array called dp of length capacity + 1 filled with zeros. Use a for loop with variable w from 1 to capacity. Inside it, use another for loop with variable i from 0 to weights.length - 1. If weights[i] is less than or equal to w, update dp[w] to the maximum of its current value and values[i] + dp[w - weights[i]].
DSA Javascript
Hint
Use dynamic programming with nested loops to fill the dp array.
4
Print the maximum value that fits in the knapsack
Print the value of dp[capacity] using console.log(dp[capacity]).
DSA Javascript
Hint
Use console.log(dp[capacity]) to print the result.
Practice
(1/5)
1. What is the key difference between Unbounded Knapsack and 0/1 Knapsack?
easy
A. Unbounded Knapsack uses recursion while 0/1 Knapsack uses iteration
B. In Unbounded Knapsack each item can be chosen any number of times; in 0/1 each item is chosen at most once
C. Unbounded Knapsack has no weight constraint
D. 0/1 Knapsack allows fractional items while Unbounded does not
Solution
Step 1: Understand 0/1 constraint
In 0/1 Knapsack each item has a binary choice - include once or exclude. After including, it cannot be reused.
Step 2: Understand Unbounded constraint
In Unbounded Knapsack there is no limit on how many times an item can be selected. The same item can fill the bag.
Step 3: Practical impact
This changes the recurrence: 0/1 uses dp[i-1][w-wt] (previous row); Unbounded uses dp[i][w-wt] (same row, enabling reuse).
Final Answer:
Unbounded allows unlimited item repetition. -> Option B
Quick Check:
Unbounded = unlimited copies. 0/1 = at most one per item. [OK]
Hint: Unbounded = unlimited copies. 0/1 = at most one copy per item. [OK]
Common Mistakes:
Confusing Unbounded with Fractional Knapsack
Thinking 0/1 allows unlimited items
Believing recursion vs iteration distinguishes them
2. In the 1D bottom-up Unbounded Knapsack solution, why does the inner loop go left-to-right (from weight[i] to W)?
medium
A. Left-to-right is faster in JavaScript due to array caching
B. Left-to-right allows the same item to be reused in one pass - exactly what Unbounded Knapsack requires
C. Right-to-left would cause negative indices
D. Left-to-right matches the order of the items array
Solution
Step 1: Left-to-right reuse
Computing dp[w] using dp[w - weight[i]] left-to-right means dp[w - weight[i]] has already been updated - it reflects using item i again. This is intentional reuse.
Step 2: Contrast with 0/1
0/1 Knapsack goes right-to-left to prevent reuse - ensuring dp[w - weight[i]] holds the pre-item value.
Final Answer:
Left-to-right enables intentional item reuse. -> Option B
Quick Check:
Left-to-right = reuse. Right-to-left = no reuse. [OK]