Climbing stairs
- CH1 prev2
- CH2 prev1
Each step's count is the sum of the two before it.
Step through it →Dynamic programming solves each smaller problem once, writes its answer in a table, and builds every bigger answer from cells already filled.
Edit distance fills its grid row by row. By the time a cell is written, the three it reads are done, so no cell is computed twice and none waits on a cell below it.
Each input here has tripped up a version of this code. The stage runs the one that gets it right.
Greedy takes 9, then 1 and 1: three coins. The table finds 5 + 6.
Each step's count is the sum of the two before it.
Step through it →Take this house plus the best from two back, or skip it.
Step through it →Extend the run that ends here, or start over at this number.
Step through it →A boundary is reachable if a word ends there from a reachable one.
Step through it →The same algorithm in plain Python. The lit line is the one the current frame came from.
def fill(coins, amount): dp = [amount + 1] * (amount + 1) dp[0] = 0 for a in range(1, amount + 1): for coin in coins: if coin <= a: dp[a] = min(dp[a], 1 + dp[a - coin]) return dpdef coin_change(coins, amount): dp = fill(coins, amount) return dp[amount] if dp[amount] <= amount else -1