0/1 Knapsack dynamic programming algorithm explained step-by-step: recursive decision tree, 2D DP state transitions, space optimization to 1D O(W), and LeetCode problems.
Given N items, each with a designated weight and profit value, along with a knapsack of maximum weight capacity W, the 0/1 Knapsack Problem seeks the subset of items that yields the maximum possible total value without exceeding capacity W. "0/1" means each item is indivisible — it must either be completely included (1) or excluded (0).
While 0/1 Knapsack is an NP-Complete problem, Dynamic Programming solves it in pseudo-polynomial O(N · W) time by caching solutions to smaller capacity subproblems. With state reduction, auxiliary space drops from a 2D matrix of size O(N · W) down to a single 1D array of size O(W) by iterating capacities backwards.
`dp[i][w]` represents the maximum profit achievable using a subset of the first `i` items with remaining knapsack weight limit `w`.
For item `i` with weight `wt` and value `val`: if `wt > w`, it cannot fit: `dp[i][w] = dp[i-1][w]`. If `wt <= w`, take the maximum between excluding it (`dp[i-1][w]`) or including it (`val + dp[i-1][w - wt]`).
Because row `i` depends exclusively on row `i - 1`, we collapse the 2D matrix into a 1D array `dp[w]`.
Traverse capacity `w` in reverse order (from `W` down to `wt`). This prevents overwriting the subproblem values from the previous item before they are consumed.
1// 1D Array Space-Optimized 0/1 Knapsack: O(N * W) Time, O(W) Space2function knapsack(weights, values, W) {3 const n = weights.length;4 // dp[w] stores maximum value achievable with capacity w5 const dp = new Array(W + 1).fill(0);6 7 for (let i = 0; i < n; i++) {8 const wt = weights[i];9 const val = values[i];10 11 // Traverse backwards from W down to wt so each item is picked AT MOST ONCE12 for (let w = W; w >= wt; w--) {13 dp[w] = Math.max(dp[w], val + dp[w - wt]);14 }15 }16 17 return dp[W];18}19 20// Example usage:21// const weights = [1, 2, 3];22// const values = [10, 15, 40];23// const W = 6;24// knapsack(weights, values, W); // Returns 65 (Items with wt 1, 2, 3 => 10 + 15 + 40)Play through every comparison, swap, and state change, adjust the speed, or enter custom inputs to test edge cases.
The full interactive roadmap is unlocking — these specialized modules land in upcoming releases.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
Explore the full catalog — 30+ algorithms with more unlocking every release.
Overlapping subproblem memoization & DP table animator
Test your understanding of 0/1 Knapsack Problem complexity and step mechanics.
Q1.Why must capacity w be traversed backwards in the 1D space-optimized 0/1 Knapsack DP array?
Q2.Why is the O(N · W) time complexity described as "pseudo-polynomial"?
Apply 0/1 Knapsack Problem to real coding interview questions.
Determine if an array can be partitioned into two subsets with equal sum.
Find number of ways to assign + and - signs to make the sum of array elements equal to target S.