Introduction to Dynamic Programming: State, Recurrence and a Worked Coin-Change Example

Learn how to design a DP solution from zero, then apply the method to an unlimited coin-change problem using recursion, tabulation and answer reconstruction.

KnowledgeGate Team

Exam prep & CS education

Updated 25 Nov 20266 min read
Introduction to Dynamic Programming: State, Recurrence and a Worked Coin-Change Example

Recognising a Fibonacci-style recurrence is not enough to solve a new dynamic programming problem. You may still be unsure what a state means, which transition is valid, or which states must be computed first. Dynamic programming can be designed through five decisions: state, transition, base case, evaluation order and answer location. The minimum-coin problem has both recursive and tabular solutions. Dynamic programming is a problem-solving method, not one fixed algorithm.

Dynamic programming basics: repeated subproblems plus reusable answers

Dynamic programming models a problem as states. It stores each solved state's answer and reuses it when the state returns. Overlapping subproblems occur when different recursive paths request the same state. Optimal substructure means an optimal answer can be built from optimal answers to smaller states.

The structure matters. Divide and conquer, as in merge sort, creates mostly independent subproblems. A greedy algorithm commits to one locally preferred choice, so it needs proof that the commitment is safe. DP instead keeps the best result for every relevant state before combining results. The contrast is developed further in Greedy Algorithms: Strategy and Problems.

The Dynamic Programming in Algorithms: Complete Guide with Worked Examples for GATE and Interviews uses a two-dimensional item-by-capacity state for 0/1 knapsack. Unlimited coin change instead needs only the remaining amount, and pick[x] reconstructs the selected coins.

Use unlimited coins {1, 3, 4} to make amount 6 with the fewest coins. A direct recursive function solve(x) repeatedly requests amounts such as 3, 2 and 1. The remaining amount is therefore a natural state.

Dynamic programming design: state, transition, base case, order and answer

A complete DP design makes five decisions explicit:

  1. State: dp[x] is the minimum number of coins needed to make amount x.

  2. Transition: choose the final coin c, then solve the remaining amount x-c.

  3. Base and invalid states: dp[0]=0, while dp[x]=infinity for x<0.

  4. Evaluation order: compute smaller non-negative amounts before larger ones.

  5. Answer location: the required result is dp[6].

For x>0, the recurrence is dp[x] = 1 + min(dp[x-1], dp[x-3], dp[x-4]), ignoring terms with negative indices. The added 1 counts the final coin selected. A recurrence is incomplete until its state meaning and base cases are stated.

This table gives only the minimum count. To recover the actual coins, also store pick[x], the coin that attained the minimum. If two choices tie, either is valid as long as the optimal value remains correct.

Memoization and tabulation: two evaluation orders for the same recurrence

Top-down memoization begins with solve(6). It returns 0 at amount 0, infinity for a negative amount, and a cached value when x has already been solved. Otherwise, it tries coins 1, 3 and 4, caches the minimum result, and returns it. The first requested answer controls which reachable states are explored.

Bottom-up tabulation starts with dp[0]=0 and initialises dp[1] through dp[6] to infinity. For each x=1,2,...,6, scan coins in the fixed order 1,3,4. Whenever x-c>=0, relax dp[x] using 1+dp[x-c]. Every smaller amount is final before a larger amount reads it.

Both methods implement the same recurrence and must return the same answer. Memoization follows recursive dependencies and can avoid unreachable states. Tabulation removes call-stack overhead and exposes the legal computation order. A cache changes repeated work, not the mathematical definition. Neither method can repair a wrong state or transition.

Side-by-side recurrence figure for coins 1, 3, and 4, showing repeated states solve(3), solve(2), and solve(1), alongside memoized states 0 through 6.

Dynamic programming worked example: minimum coins for amount 6

Keep the coins unlimited and fixed at {1,3,4}. An invalid predecessor is impossible, so it must never be treated as a free zero-cost state.

x

Candidate with coin 1

Candidate with coin 3

Candidate with coin 4

dp[x]

pick[x]

0

base case

base case

base case

0

none

1

1+dp[0]=1

invalid

invalid

1

1

2

1+dp[1]=2

invalid

invalid

2

1

3

1+dp[2]=3

1+dp[0]=1

invalid

1

3

4

1+dp[3]=2

1+dp[1]=2

1+dp[0]=1

1

4

5

1+dp[4]=2

1+dp[2]=3

1+dp[1]=2

2

1

6

1+dp[5]=3

1+dp[3]=2

1+dp[2]=3

2

3

The fixed scan order resolves the tie at x=5 by recording coin 1, though coin 4 would give the same value. At x=6, the unique minimum candidate is 1+dp[3]=2, so pick[6]=3.

Each row reads only already final smaller amounts.

Reconstruct from 6: take coin 3 and move to 6-3=3; take coin 3 again and move to 3-3=0. The optimum is therefore two coins, {3,3}. A largest-coin-first shortcut chooses 4+1+1, which uses three coins. This counterexample shows why that greedy commitment is unsafe for these denominations.

Bottom-up DP table for target 6 with values 0, 1, 2, 1, 1, 2, 2, showing optimal reconstruction 6 to 3 to 0 and two coins versus three.

Why the DP recurrence is correct and how much work it saves

Prove the recurrence by induction on amount x. The base x=0 needs zero coins. For x>0, every valid solution ends with some coin c, leaving x-c. If that remainder were not optimal, replacing it with a better solution would improve the original. Thus the minimum over all legal final coins is optimal for x.

For target A and m coin types, there are A+1 states. Each checks at most m coins, so both methods take O(mA) worst-case time and O(A) answer storage. With coin 1, top-down recursion can also use O(A) stack space. Uncached recursion revisits amounts along many paths.

Dynamic programming mistakes: what goes wrong and how to repair it

Mistake

What goes wrong

Repair

Leaving dp[x] undefined

The formula can answer a different question

Define it as minimum coins for amount x

Using dp[0]=1

The empty amount is charged

Use dp[0]=0; an empty selection uses no coins

Treating a negative state as zero

An impossible predecessor looks free

Use infinity or skip that transition

Confusing minimum coins with combinations

min cannot count combinations

Use a counting state and addition

Filling a state before its dependencies

A transition reads unfinished values

Process smaller amounts first

Skipping the memoization cache check

Repeated states are recomputed

Return cached answers before branching

This unbounded example permits reuse: 3+3. A 0/1 variant needs an item dimension or capacity order preventing reuse. “Take the largest coin” fails here, returning three coins instead of two.

How exams test an introduction to dynamic programming

Cue

What your answer must show

Define the state

Explain what every index represents

Derive the recurrence

Name the final choice and its predecessor

Fill the table

Respect the dependency order

Compare memoization and tabulation

Discuss evaluation order, stack use and storage

Find complexity

Count states multiplied by work per state

DP or greedy

Test whether a local commitment can fail

Use the worked example for rapid checks. dp[4]=1. The chosen predecessor for dp[6] is dp[3] through coin 3. The final answer is 2, not 3. Overlapping subproblems explains why stored answers can be reused, while optimal substructure justifies building an optimum from smaller optimal answers.

Practise with about 20 Introduction to DP questions available in our practice bank. The GATE CS Exam Preparation page is the broader subject-navigation route.

Dynamic programming in the short version and the next step

Ask five questions: What does the state mean? Which choice creates the transition? What are the base and invalid states? In what order are dependencies available? Where is the final answer? Here, dp[0..6]=[0,1,2,1,1,2,2], and the optimum is 3+3.

Next, Dynamic Programming Explained: 0/1 Knapsack adds an item index and a take-or-skip choice. For a structured route through broader GATE CS preparation, GATE Guidance by Sanchit Sir is an optional next step.