Dynamic Programming (DP)
- Climbing Stairs
Problem Statement:
You are climbing a staircase that has
nsteps.
Each time you can climb either 1 step or 2 steps.Your task: Count how many distinct ways you can reach the top.
Input:
An integer
nrepresenting the total number of steps (n ≥ 0)
Output:
An integer representing the number of distinct ways to climb to the top
Examples:
Input ( n)Output Explanation 1 1 Only one way: [1] 2 2 [1+1], [2] 3 3 [1+1+1], [1+2], [2+1] 4 5 [1+1+1+1], [2+2], [1+1+2], [1+2+1], [2+1+1] - Solution – https://gitlab.com/gsugiart/usaco_curriculum/-/blob/master/src/ClimbingStairs.java?ref_type=heads
- Coin Change (Count the Number of Ways)
You are given a list of coin denominations and a target amount.
Your task: Count how many different ways you can make up that amount using unlimited coins from the list.
You may use each coin as many times as you want.
Input:
An integer
amount(amount ≥ 0)An array
coins[]of positive integers
Output:
An integer representing the number of distinct combinations to make up the
amount
Examples:
Example 1:
✅ Output:
4✅ Explanation:
[1+1+1+1+1]
[1+1+1+2]
[1+2+2]
[5]
Example 1 explanation
Goal:
dp[i]represents the number of combinations to make amounti.You iterate over each
coin, and for each coin, you build up the ways to make amounts fromcoinup toamount.
Step-by-Step Example:
Let’s use:
Initialize:
First Pass: coin = 1
Update from index 1 to 5:
dp[1] += dp[0]→ 1dp[2] += dp[1]→ 1dp[3] += dp[2]→ 1dp[4] += dp[3]→ 1dp[5] += dp[4]→ 1
Now:
➡️ All amounts can be made with just 1-coin combinations.
Second Pass: coin = 2
Update from index 2 to 5:
dp[2] += dp[0]→ 1 + 1 = 2dp[3] += dp[1]→ 1 + 1 = 2dp[4] += dp[2]→ 1 + 2 = 3dp[5] += dp[3]→ 1 + 2 = 3
Now:
➡️ Includes combinations using coins of 1 and 2.
Third Pass: coin = 5
Update only
dp[5]:dp[5] += dp[0]→ 3 + 1 = 4
Now:
➡️ You now also include combinations that use the 5-coin.
✅ Final Answer:
There are 4 combinations to make 5:
1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 2
1 + 2 + 2
5
Why the Loop Order Matters
You loop over coins first, then amounts, to ensure:
Each combination is only counted once
You avoid counting permutations like
1+2+2and2+1+2as different
This makes it a combinations solution, not permutations.
- Solution – https://gitlab.com/gsugiart/usaco_curriculum/-/blob/master/src/CoinChange.java?ref_type=heads
- Climbing Stairs