Dynamic-programming

53 articles

dsa1 min read

Climbing Stairs — Fibonacci DP

Count ways to climb n stairs taking 1 or 2 steps. Classic Fibonacci DP — dp[n] = dp[n-1] + dp[n-2] with O(1) space.

Read →
dsa1 min read

House Robber — Skip Adjacent DP

Maximize amount robbed without robbing adjacent houses. Classic skip-one DP: dp[i] = max(dp[i-1], dp[i-2] + nums[i]).

Read →
dsa1 min read

House Robber II — Circular Array DP

Houses arranged in a circle: first and last are adjacent. Run linear House Robber twice: once excluding last house, once excluding first.

Read →
dsa1 min read

Maximum Subarray — Kadane's Algorithm

Find the contiguous subarray with the largest sum. Kadane's algorithm: either extend previous subarray or start fresh at current element.

Read →
dsa2 min read

Coin Change — Unbounded Knapsack DP

Find minimum coins to make amount. Unbounded knapsack DP: dp[amount] = min(dp[amount-coin]+1) over all coins. Bottom-up from 0 to amount.

Read →
dsa1 min read

Jump Game — Greedy Reachability

Check if you can reach the last index. Greedy: track furthest reachable index. If current position > reach, stuck.

Read →
dsa1 min read

Decode Ways — Counting DP

Count ways to decode a digit string as letters (A=1,...,Z=26). DP: single digit + valid double digit transitions.

Read →
dsa1 min read

Word Break — Reachability DP

Check if string can be segmented into dictionary words. DP: dp[i]=true if some dp[j] is true and s[j:i] is in wordDict.

Read →
dsa1 min read

Russian Doll Envelopes — 2D LIS

Maximum envelopes you can nest. Sort by width ascending and height descending, then LIS on heights. Descending heights prevents using same-width twice.

Read →
dsa1 min read

Target Sum — DP Count Ways

Assign + or - to each number to reach target sum. DP counts ways to reach each sum. Reduces to 0/1 knapsack subset sum count.

Read →
dsa1 min read

Unique Paths — Grid Path Count DP

Count paths from top-left to bottom-right moving only right or down. dp[i][j] = dp[i-1][j] + dp[i][j-1]. Math formula O(1) also works.

Read →
dsa1 min read

Minimum Path Sum — Grid DP

Find minimum cost path from top-left to bottom-right moving right or down. dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]).

Read →
dsa1 min read

Triangle — Bottom-Up 1D DP

Minimum path sum from top to bottom of triangle. Bottom-up DP: dp[j] = triangle[i][j] + min(dp[j], dp[j+1]).

Read →
dsa1 min read

Edit Distance — Wagner-Fischer DP

Minimum operations (insert, delete, replace) to transform word1 to word2. Classic Wagner-Fischer 2D DP with O(n) space optimization.

Read →
dsa1 min read

Burst Balloons — Interval DP

Maximize coins by bursting balloons. Key insight: think of last balloon burst in interval [i,j]. dp[i][j] = max coins from interval with k as last balloon.

Read →
dsa1 min read

Interleaving String — 2D DP

Check if s3 is formed by interleaving s1 and s2. dp[i][j] = can s3[:i+j] be formed from s1[:i] and s2[:j].

Read →
dsa1 min read

Regular Expression Matching — 2D DP

Match string s against pattern p with . and *. dp[i][j] = does s[:i] match p[:j]. Handle * by matching zero or more of preceding char.

Read →
dsa1 min read

Wildcard Matching — 2D DP

Match string with wildcard pattern: ? matches any char, * matches any sequence. Similar to regex but * matches any sequence (not zero/more of preceding).

Read →
dsa1 min read

Dungeon Game — Reverse Grid DP

Find minimum initial health to reach bottom-right dungeon cell. Solve backwards: dp[i][j] = min health needed at (i,j) to survive.

Read →
dsa1 min read

Ones and Zeroes — 2D 0/1 Knapsack

Find maximum strings using at most m zeros and n ones. 2D 0/1 knapsack: dp[i][j] = max strings using i zeros and j ones.

Read →
dsa1 min read

Strange Printer — Interval DP

Minimum turns to print string where each turn prints a contiguous run of same char. Interval DP: dp[i][j] = min turns for s[i..j].

Read →