0
0
DSA Typescriptprogramming~10 mins

Coin Change Minimum Coins in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Coin Change Minimum Coins
Start with amount
Initialize DP array with large values
Set dp[0
For each coin
For each amount from coin to target
Update dp[amount
After filling dp array
If dp[target
End
We build a table dp where dp[i] stores the minimum coins needed for amount i. We update dp by trying each coin for all amounts.
Execution Sample
DSA Typescript
const coins = [1, 3, 4];
const amount = 6;
const dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (const coin of coins) {
  for (let x = coin; x <= amount; x++) {
    dp[x] = Math.min(dp[x], dp[x - coin] + 1);
  }
}
Calculate minimum coins needed to make amount 6 using coins 1, 3, and 4.
Execution Table
StepOperationAmount Index (x)Coin Useddp[x] Beforedp[x - coin]dp[x] Afterdp Array State
1Initialize dp----0[0, ∞, ∞, ∞, ∞, ∞, ∞]
2Use coin 111dp[0]=01[0, 1, ∞, ∞, ∞, ∞, ∞]
3Use coin 121dp[1]=12[0, 1, 2, ∞, ∞, ∞, ∞]
4Use coin 131dp[2]=23[0, 1, 2, 3, ∞, ∞, ∞]
5Use coin 141dp[3]=34[0, 1, 2, 3, 4, ∞, ∞]
6Use coin 151dp[4]=45[0, 1, 2, 3, 4, 5, ∞]
7Use coin 161dp[5]=56[0, 1, 2, 3, 4, 5, 6]
8Use coin 3333dp[0]=01[0, 1, 2, 1, 4, 5, 6]
9Use coin 3434dp[1]=12[0, 1, 2, 1, 2, 5, 6]
10Use coin 3535dp[2]=23[0, 1, 2, 1, 2, 3, 6]
11Use coin 3636dp[3]=12[0, 1, 2, 1, 2, 3, 2]
12Use coin 4442dp[0]=01[0, 1, 2, 1, 1, 3, 2]
13Use coin 4543dp[1]=12[0, 1, 2, 1, 1, 2, 2]
14Use coin 4642dp[2]=22[0, 1, 2, 1, 1, 2, 2]
15End-----[0, 1, 2, 1, 1, 2, 2]
💡 Finished all coins and amounts; dp[6] = 2 means minimum 2 coins needed.
Variable Tracker
VariableStartAfter Step 2After Step 8After Step 12Final
dp[0, ∞, ∞, ∞, ∞, ∞, ∞][0, 1, 2, 3, 4, 5, 6][0, 1, 2, 1, 2, 3, 2][0, 1, 2, 1, 1, 2, 2][0, 1, 2, 1, 1, 2, 2]
coin-134-
x (amount index)-1 to 63 to 64 to 6-
Key Moments - 3 Insights
Why do we initialize dp array with Infinity except dp[0]?
We use Infinity to represent amounts not yet reachable. dp[0] = 0 means zero coins needed for amount zero. See execution_table row 1 where dp is initialized.
Why do we update dp[x] with dp[x - coin] + 1?
Because if we can make amount (x - coin) with dp[x - coin] coins, adding one coin of current coin value makes amount x. This is shown in rows 2-14 where dp[x] updates based on dp[x - coin].
Why does dp[6] end up as 2 and not a larger number?
Because using coins 3 and 3 or 4 and 2 coins, minimum coins to make 6 is 2. The dp array updates to smaller values as better combinations are found, shown in rows 11, 14, and final dp state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is dp[3] after using coin 3?
A3
B1
C
D0
💡 Hint
Check the 'dp[x] After' column at step 8 for amount index 3.
At which step does dp[4] first update to 1?
AStep 12
BStep 9
CStep 5
DStep 14
💡 Hint
Look at dp array state changes in steps 9 and 12 for dp[4].
If coin 2 was added, how would dp[6] change compared to final dp?
Adp[6] would remain 2
Bdp[6] would increase
Cdp[6] would decrease
Ddp[6] would become Infinity
💡 Hint
Adding coin 2 gives more ways to form 6 with fewer coins, so dp[6] could decrease.
Concept Snapshot
Coin Change Minimum Coins:
- Use dp array of size amount+1, fill with Infinity
- dp[0] = 0 (0 coins for amount 0)
- For each coin, update dp[x] = min(dp[x], dp[x - coin] + 1)
- dp[amount] gives minimum coins or Infinity if no solution
- Bottom-up dynamic programming approach
Full Transcript
This visualization shows how to find the minimum number of coins needed to make a target amount using given coin denominations. We create a dp array where each index represents an amount from 0 to the target. We start with dp[0] = 0 because zero coins are needed for amount zero, and all other dp values are set to Infinity to indicate unreachable amounts. Then, for each coin, we update the dp array for amounts from the coin's value up to the target. We check if using the coin reduces the number of coins needed for that amount by comparing dp[x] with dp[x - coin] + 1. After processing all coins, dp[amount] holds the minimum coins needed or Infinity if no combination is possible. The execution table traces each update step, showing how dp values improve over time. Key moments clarify why initialization uses Infinity, how updates work, and why the final dp value represents the answer. The quiz tests understanding of dp updates and effects of adding coins.