HomeDynamic Programming
Dynamic Programming
Overlapping subproblems — solve each once, reuse forever.
65 shown
- FundamentalsOverlapping subproblems · a table you fill onceanimatedEasy
- Solving a Question with DPState → recurrence → base case → order → answeranimatedEasy
- Climbing Stairsways(i) = ways(i−1) + ways(i−2) · FibonaccianimatedEasy
- Maximum Subarraycur = max(nums[i], cur + nums[i]) · Kadane'sanimatedMedium
- House Robberdp[i] = max(dp[i−1], dp[i−2] + nums[i])animatedMedium
- Coin Changedp[a] = 1 + min(dp[a − coin]) · fewest coinsanimatedMedium
- Longest Common SubsequenceGrid DP · match → diagonal+1, else max(up, left)animatedMedium
- Unique PathsGrid DP · dp[i][j] = dp[i−1][j] + dp[i][j−1]animatedMedium
- Longest Increasing SubsequencePatience sorting · O(n log n)animatedMedium
- Word Breakdp[i] = can s[0..i) split into dictionary wordsanimatedMedium
- Practice problems
- Frog JumpBest of stepping one or twoanimatedMedium
- Frog jump with K distancesBest over the previous k stepsanimatedMedium
- Maximum sum of non adjacent elementsTake it and skip one, or skip itanimatedMedium
- Ninja's trainingState includes yesterday's activityMedium
- Grid Unique Paths : DP on GridsPaths in = paths from above + paths from the leftanimatedMedium
- Unique paths IIAn obstacle contributes zero pathsanimatedMedium
- Minimum Falling Path SumBest of three cells aboveMedium
- TriangleFill upward from the baseMedium
- Ninja and his FriendsTwo positions, one shared rowMedium
- Subset sum equal to targetTake or skip, indexed by remaining targetanimatedHard
- Partition equal subset sumSubset sum for half the totalanimatedHard
- Partition a set into two subsets with minimum absolute sum differenceFind every reachable subset sumanimatedHard
- Count subsets with sum KAdd the two branches instead of OR-ing themanimatedHard
- Count partitions with given differenceSolve for one subset's sumanimatedHard
- Assign CookiesSort both, match greedilyEasy
- Minimum CoinsUnbounded: stay on the same coinanimatedHard
- Target sumSigns become a subset choiceanimatedHard
- Coin Change 2Coins in the outer loop, or you count ordersanimatedHard
- Unbounded knapsackTaking an item does not consume itanimatedHard
- Rod Cutting ProblemUnbounded knapsack in disguiseanimatedHard
- Print Longest Common SubsequenceWalk the table backwardsanimatedHard
- Longest common substringReset to zero on a mismatchanimatedHard
- Longest palindromic subsequenceLCS of the string with its reverseanimatedHard
- Minimum insertions to make string palindromeKeep the longest palindromic coreanimatedHard
- Minimum insertions or deletions to convert string A to BEverything outside the LCS must changeanimatedHard
- Shortest common supersequenceBoth strings, sharing the LCS onceanimatedHard
- Distinct subsequencesMatch or skip the character of sanimatedHard
- Edit distanceInsert, delete or replace — take the cheapestanimatedHard
- Wildcard matching'*' either consumes a character or nothinganimatedHard
- Best time to buy and sell stockCheapest so far, best profit so faranimatedMedium
- Best time to buy and sell stock IICollect every upward moveMedium
- Best time to buy and sell stock IIIFour states across the dayMedium
- Best time to buy and sell stock IVThe four-state machine, generalised to kMedium
- Best Time to Buy and Sell Stock with CooldownThree states: holding, sold, freeMedium
- Best time to buy and sell stock with transaction feesCharge the fee once per transactionMedium
- Print Longest Increasing SubsequenceStore a predecessor with each lengthanimatedMedium
- Largest Divisible SubsetSort, then LIS with a divisibility testanimatedMedium
- Longest String ChainSort by length, extend by one characteranimatedMedium
- Longest Bitonic SubsequenceLIS from the left, LIS from the rightanimatedMedium
- Number of Longest Increasing SubsequencesCarry a count alongside each lengthanimatedMedium
- Matrix chain multiplicationTry every split pointHard
- Minimum cost to cut the stickInterval DP over the cut positionsHard
- Burst balloonsChoose the balloon burst lastHard
- Different Ways to Evaluate a Boolean ExpressionCount true and false ways separatelyMedium
- Palindrome partitioning IICut where the prefix is a palindromeHard
- Partition Array for Maximum SumTry every group length ending hereMedium
- Maximum Rectangle Area with all 1's|A histogram per rowHard
- Count Square Submatrices with All Ones|Each cell counts the squares ending thereEasy
- Solving a Question with Dynamic ProgrammingState, recurrence, base case, orderanimatedMedium
- Counting BitsReuse the answer for half the numberMedium
- Decode WaysOne digit or two, if validMedium
- Maximal SquareThe same recurrence, take the maximumMedium
- Maximum Profit in Job SchedulingSort by end, binary search the last compatible jobMedium
- Paint HouseBest cost per colour, per houseMedium
- Paint House IITrack the best two previous coloursMedium