All puzzles › Dynamic Programming
Dynamic Programming Practice Problems
13 puzzles (1 easy, 6 medium, 6 hard), 2 free, in Python, JavaScript and C++.
Dynamic programming is what you do when a problem breaks into smaller versions of itself and those smaller versions keep coming up again. You solve each one once, write the answer down, and look it up every time after. The work is almost all in deciding what one entry of the table means; once that sentence is right, the code follows.
How to spot a dynamic programming problem
Look for counting the ways to do something, or the best total you can reach, where each step depends on a few earlier ones. If a plain recursive solution is correct but repeats the same calls over and over, remembering the results turns exponential time into linear.
New to it? Read the lesson first: Dynamic Programming.
The puzzles, easiest first
- Climbing Stairs (Easy): Given n stairs, return how many different ways you can climb to the top taking 1 or 2 steps at a time.
- Let the Reward Spread (Medium, free): An agent walks a row of cells, and each cell pays a reward that is never negative.
- Maximum Subarray (Medium, free): Given an array of numbers, return the largest sum of any contiguous run.
- Pascal's Triangle (Medium): Given numRows, return the first numRows rows of Pascal's triangle.
- Spend the Mana (Medium): Each spell has a mana cost and a damage, and each can be cast once.
- Take the Least Damage (Medium): A hero crosses a trapped room from the top-left tile to the bottom-right, moving only right or down.
- Unique Grid Paths (Medium): Given an m by n grid, return how many routes a robot can take from the top-left corner to the bottom-right if it can only move right or down.
- 0/1 Knapsack (Hard): Given item weights, item values, and a weight limit, return the most value you can carry.
- Coin Change (Fewest) (Hard): Given coin values and an amount, return the fewest coins that add up to it exactly, or -1 if you can't.
- Edit Distance (Hard): Given two words, return the fewest single-letter inserts, deletes, or swaps that turn one into the other.
- Longest Increasing Subsequence (Hard): Given an array, return the length of the longest strictly increasing subsequence.
- Minimum Path Sum (Hard): Given a grid of costs, return the cheapest total to get from the top-left to the bottom-right, moving only right or down.
- Word Break (Hard): Given a string and a list of dictionary words, return true if the string can be split completely into dictionary words.
Practice another pattern
Or browse by subject
Browse all 676 puzzles