Workspace Coin Change Ii
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Coin Change II visualizer

Learn Coin Change II with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice subsequences and knapsack.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 11Execution step
Reference example · JavaScript
Inspect reference input
{
  "coins": [
    1,
    2,
    5
  ],
  "k": 5
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
coins,k
},s)=>{
let d=Array(k+1).fill(0);
d[0]=1;
for(let c of coins)for(let i=c;
i<=k;
i++){
d[i]+=d[i-c];
s("Add ways using coin "+c,{
DP:d
},[i])
}
return d[k]
};

Execution canvas

Active Changed
STEP 1

Add ways using coin 1

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

DP6 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · Subsequences and knapsack

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "coins": [
    1,
    2,
    5
  ],
  "k": 5
}

Execution steps

  1. Add ways using coin 1
  2. Add ways using coin 2
  3. Add ways using coin 5

Computed output

4
JavaScript reference implementation
const solve = ({
  coins,k
},s)=>{
  let d=Array(k+1).fill(0);
  d[0]=1;
  for(let c of coins)for(let i=c;
  i<=k;
  i++){
    d[i]+=d[i-c];
    s("Add ways using coin "+c,{
      DP:d
    },[i])
  }
  return d[k]
};

Continue learning

Practice related DSA questions in the same topic.