This visual guide is designed for students who understand some code but need a stronger mental picture of how the data structure or algorithm behaves. Use the diagram first, then connect it to code.
DP stores answers to smaller states
| State | Meaning | Uses |
|---|---|---|
| dp[0] | ways to make 0 | base case |
| dp[3] | ways to make 3 | dp[2], dp[1] |
| dp[n] | answer for target n | earlier states |
Mental model
Dynamic programming works when the same smaller questions appear again and again. Greedy works when a local best choice can be proven to stay globally safe.
How it shows up in code
For counting ways to reach a score, dp[i] can mean the number of ways to reach exactly i points. Each entry uses earlier entries. A greedy solution would need a proof that the next best-looking choice never blocks the final answer.
What to trace
Practice prompt
Define the state and transition for counting ways to climb stairs using 1-step, 2-step, and 5-step moves. Then ask whether a greedy rule would work.
When students need support
Code Scholars helps students turn the visual model into implementation, tests, edge-case reasoning, and runtime analysis. This is especially useful when students can follow an explanation but struggle to write the code independently.
