This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 4: Optimisation
Two strings, a grid, or an index plus a budget: the state needs two numbers.
Builds on 1-D Dynamic Programming.
The question to ask
Does describing a subproblem take two numbers, like a position in each string or a row and a column?
When one index can't describe where you are, the table grows a dimension. Two strings give dp[i][j] over prefixes of each (Longest Common Subsequence, Edit Distance). A grid gives dp[r][c] built from above and left (Unique Paths). An index plus a remaining budget is a knapsack (Coin Change II, Target Sum). The method is the same as 1-D: define the cell in words, write how it comes from its neighbours, set the first row and column, and fill in an order where those neighbours already exist. Most of these compress to one row.
Want to watch it run first? Step through the interactive walkthrough.
Time O(m × n) · Space O(m × n), often O(n) with one rolling row
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
dp[i][j] is the fewest operations turning the first i of a into the first j of b. Matching characters cost nothing; otherwise it's one plus the best of replace (diagonal), delete (above) or insert (left). The first row and column are all inserts or all deletes.
function minDistance(a: string, b: string): number {
const dp = Array.from({ length: a.length + 1 }, (_, i) =>
Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)),
);
for (let i = 1; i <= a.length; i++) {
for (let j = 1; j <= b.length; j++) {
dp[i][j] = a[i - 1] === b[j - 1]
? dp[i - 1][j - 1]
: 1 + Math.min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[a.length][b.length];
}Unique Paths: each cell is the sum of above and left. Keep one row; row[c] still holds 'above' when you read it, and row[c - 1] has just become 'left'.
function uniquePaths(m: number, n: number): number {
const row = new Array<number>(n).fill(1);
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) row[c] += row[c - 1];
}
return row[n - 1];
}Coin Change II counts combinations, not orderings. Looping coins in the outer loop means each coin is considered once, in order, so 1 + 2 and 2 + 1 are the same combination. Swap the loops and you count permutations instead.
function change(amount: number, coins: number[]): number {
const ways = new Array<number>(amount + 1).fill(0);
ways[0] = 1;
for (const coin of coins) {
for (let a = coin; a <= amount; a++) ways[a] += ways[a - coin];
}
return ways[amount];
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Generate the first numRows rows of Pascal's triangle, where each number is the sum of the two numbers directly above it.
Each cell from the row above: the gentlest two-dimensional table there is.
Not attempted yet
A robot moves only right or down on an m by n grid. How many unique paths reach the bottom-right corner?
"Unique paths" moving "right or down": above plus left, in one rolling row.
Not attempted yet
Moving only right or down, find a path from top-left to bottom-right that minimises the path sum of the grid.
"Path sum" moving "right or down": each cell plus the min of above and left.
Not attempted yet
Given two strings, return the length of their longest common subsequence.
"Two strings" and a "common subsequence": the template.
Not attempted yet
Return the number of combinations that make up the amount using unlimited coins of each denomination.
"Combinations that make up" an amount: coins in the outer loop so order doesn't count twice.
Not attempted yet
Put a + or - before each number. Return the number of ways the expression evaluates to the target sum.
"Target sum" with signs: count subsets summing to (total + target) / 2, a counting knapsack.
Not attempted yet
Return the minimum number of operations (insert, delete, replace a character) to convert word1 into word2.
"Minimum number of operations" between "two strings": "edit distance", with diagonal, above and left neighbours.
Not attempted yet
Is s3 formed by an interleaving of s1 and s2, keeping each string's characters in order?
"Interleaving": dp[i][j] says whether i chars of s1 and j of s2 build the first i + j of s3.
Not attempted yet
After selling you must wait one day (a cooldown) before buying again. Return the maximum profit.
"Cooldown": three states per day (holding, just sold, resting) and the transitions between them.
Not attempted yet
In a binary matrix, find the largest square containing only 1s and return its area.
Largest "square" in a "matrix": a cell's side is 1 + the min of its top, left and top-left neighbours.
Not attempted yet
Return the length of the longest strictly increasing path in a matrix, moving in four directions.
Four directions means no fill order, but strictly increasing means no cycles: DFS from each cell with a memo.
Not attempted yet
Return the number of distinct subsequences of s that equal t.
"Distinct subsequences" of one string matching another: dp over both prefixes, adding the match and skip cases.
Not attempted yet
Bursting balloon i earns nums[left] × nums[i] × nums[right] coins. Return the maximum coins from bursting all of them.
Interval DP: choose which balloon bursts last inside (l, r), so its neighbours are fixed as l and r.
Not attempted yet
Implement regular expression matching with '.' and '*' over the entire input string.
"Regular expression" with '*': dp over string and pattern positions; '*' means zero copies (skip two in the pattern) or one more.
Not attempted yet