Skip to content

0/1 Knapsack DP Table
Step by Step

Every cell keeps the better of two choices, which are skipping the item or taking it plus the top value of the leftover capacity.

Step forward and back through the table, then share a link to the exact step that confused you.

What You Watch While the Table Fills

Every cell answers one question, and the visualiser shows how.

Every cell holds the top value for the items so far at one capacity.

CodeStory's visualiser fills the table cell by cell and draws the call tree beside it. Code lines stay highlighted as the picture changes, so you can step forward, step back or let it play.

Open Classic first, where the most valuable item stays out at capacity 7. Then load Greedy trap to see why top value per weight fails.

How the DP Table Fills Cell by Cell

These steps match the table on your screen.

  1. 1Build a table with one row per item and one column per capacity from 0 to W.
  2. 2Copy the cell above when the item weighs more than the capacity.
  3. 3Otherwise compare skipping the item with taking it, which adds its value to the row above at capacity minus weight.
  4. 4Store the larger value in the cell.
  5. 5Find the answer in the last row and the last column.

Where 0/1 Knapsack Goes Wrong

The switch lives in the visualiser. Flip it on and watch the table break.

  • Bug: dp[i][w − wt]Reading the item's own row instead of the row above lets one item go in again and again. Load One item, lots of room to see it.

0/1 Knapsack Inputs Worth Trying

Six presets cover the greedy trap, empty capacity and the bug.

  • ClassicWatch row D at capacity 7, where the most valuable item stays out.
  • Greedy trapA has the top value per weight, yet B + C beat it.
  • Capacity 0One column and nothing can go in.
  • Nothing fitsEvery row copies the row above.
  • Everything fitsAll three items go in. Switch on the bug here too.
  • One item, lots of roomSwitch on the dp[i][w − wt] bug and CodeStory packs A three times.

0/1 Knapsack Cost in Time and Space

Interviewers ask why the table needs n times W cells, and the visualiser counts real operations.

Time
O(n·W)counted from real operations as the table fills
Space
O(n·W)extra memory for the table besides the input
Pattern
Recursion & DPSmaller copies get solved once and reused.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

0/1 Knapsack Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the 0/1 knapsack visualiser show?

CodeStory's visualiser fills the DP table cell by cell on items you choose, with the call tree and the highlighted line of code beside it. Step forward, step back or let it play.

What is the time complexity of 0/1 knapsack?

O(n·W) time and O(n·W) space, where n is the item count and W is the capacity. CodeStory's visualiser counts real operations as it plays, so you watch the number grow instead of trusting the formula.

Why does greedy fail on 0/1 knapsack?

Greedy takes the top value per weight first, and the Greedy trap preset shows it losing to B + C. DP tables check both choices for every item, so no better pair slips past.

Why does my 0/1 knapsack code pack an item twice?

Reading dp[i][w − wt] from the item's own row lets one item go in again and again. Switch on that bug in CodeStory's visualiser and load One item, lots of room to watch A pack three times.

Can I use my own input?

Yes. Enter your own items and a capacity, and the visualiser fills the table for them.

Is the visualiser free?

Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.

Stuck on Dynamic Programming? Get a Free Callback

Tell CodeStory where the DP table loses you, and a mentor plans your next steps.

  1. 1CodeStory checks your level, from first recursion to interview ready.
  2. 2CodeStory explains online and campus batches, with timings that fit your day.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.