Dynamic Programming Mastery
DP is the single most-searched weak spot in interview prep. 60 questions covering every DP shape, from 1D sequences to advanced state-machine problems.
1D DP
Climbing Stairs
easyYou are climbing a staircase.
Counting Bits
easyGiven an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Divisor Game
easyAlice and Bob take turns playing a game, with Alice starting first.
Fibonacci Number
easyThe Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
Is Subsequence
easyGiven two strings s and t, return true if s is a subsequence of t, or false otherwise.
Longest Unequal Adjacent Groups Subsequence I
easyYou are given a string array words and a binary array groups both of length n.
Maximum Repeating Substring
easyFor a string sequence, a string word is k-repeating if word concatenated k times is a substring of sequence.
Min Cost Climbing Stairs
easyYou are given an integer array cost where cost[i] is the cost of ith step on a staircase.
N-th Tribonacci Number
easyThe Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
2 Keys Keyboard
mediumThere is only one character 'A' on the screen of a notepad.
Airplane Seat Assignment Probability
mediumn passengers board an airplane with exactly n seats.
Apply Operations to Make Two Strings Equal
mediumYou are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.
2D & Grid DP
Pascal's Triangle
easyGiven an integer numRows, return the first numRows of Pascal's triangle.
Pascal's Triangle II
easyGiven an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
01 Matrix
mediumGiven an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
Disconnect Path in a Binary Matrix by at Most One Flip
mediumYou are given a 0-indexed m x n binary matrix grid.
Maximum Amount of Money Robot Can Earn
mediumYou are given an m x n grid.
Maximum Difference Score in a Grid
mediumYou are given an m x n matrix grid consisting of positive integers.
Maximum Non Negative Product in a Matrix
mediumYou are given a m x n matrix grid.
Maximum Number of Moves in a Grid
mediumYou are given a 0-indexed m x n matrix grid consisting of positive integers.
Minimum Moves to Spread Stones Over Grid
mediumYou are given a 0-indexed 2D integer matrix grid of size 3 * 3, representing the number of stones in each cell.
Minimum Path Cost in a Grid
mediumYou are given a 0-indexed m x n integer matrix grid consisting of distinct integers from 0 to m * n - 1.
Minimum Path Sum
mediumGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Triangle
mediumGiven a triangle array, return the minimum path sum from top to bottom.
Knapsack & Subset Sum
Coin Change
mediumYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Coin Change II
mediumYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Combination Sum IV
mediumGiven an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.
Count the Number of Square-Free Subsets
mediumYou are given a positive integer 0-indexed array nums.
Find Two Non-overlapping Sub-arrays Each With Target Sum
mediumYou are given an array of integers arr and an integer target.
Inverse Coin Change
mediumYou are given a 1-indexed integer array numWays, where numWays[i] represents the number of ways to select a total amount i using an infinite supply of some fixed coin denominations.
Largest Divisible Subset
mediumGiven a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies: If there are...
Number of Dice Rolls With Target Sum
mediumYou have n dice, and each dice has k faces numbered from 1 to k.
Partition Equal Subset Sum
mediumGiven an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
Partition to K Equal Sum Subsets
mediumGiven an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.
Target Sum
mediumYou are given an integer array nums and an integer target.
The Number of Beautiful Subsets
mediumYou are given an array nums of positive integers and a positive integer k.
Interval & Partition DP
Check if There is a Valid Partition For The Array
mediumYou are given a 0-indexed integer array nums.
Count Partitions With Max-Min Difference at Most K
mediumYou are given an integer array nums and an integer k.
Minimum Substring Partition of Equal Character Frequency
mediumGiven a string s, you need to partition it into one or more balanced substrings.
Palindrome Partitioning
mediumGiven a string s, partition s such that every substring of the partition is a palindrome.
Partition Array for Maximum Sum
mediumGiven an integer array arr, partition the array into (contiguous) subarrays of length at most k.
Partition Array to Minimize XOR
mediumYou are given an integer array nums and an integer k.
Partition String Into Minimum Beautiful Substrings
mediumGiven a binary string s, partition the string into one or more substrings such that each substring is beautiful.
Partition String Into Substrings With Values at Most K
mediumYou are given a string s consisting of digits from 1 to 9 and an integer k.
Burst Balloons
hardYou are given n balloons, indexed from 0 to n - 1.
Maximize the Number of Partitions After Operations
hardYou are given a string s and an integer k.
Minimum Cost to Merge Stones
hardThere are n piles of stones arranged in a row.
Number of Beautiful Partitions
hardYou are given a string s that consists of the digits '1' to '9' and two integers k and minLength.
Advanced & State-Machine DP
Best Time to Buy and Sell Stock
easyYou are given an array prices where prices[i] is the price of a given stock on the ith day.
Best Time to Buy and Sell Stock II
mediumYou are given an integer array prices where prices[i] is the price of a given stock on the ith day.
Best Time to Buy and Sell Stock V
mediumYou are given an integer array prices where prices[i] is the price of a stock in dollars on the ith day, and an integer k.
Best Time to Buy and Sell Stock with Cooldown
mediumYou are given an array prices where prices[i] is the price of a given stock on the ith day.
Best Time to Buy and Sell Stock with Transaction Fee
mediumYou are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.
Count Numbers with Unique Digits
mediumGiven an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.
Number of Smooth Descent Periods of a Stock
mediumYou are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the ith day.
Paint House IV
mediumYou are given an even integer n representing the number of houses arranged in a straight line, and a 2D array cost of size n x 3, where cost[i][j] represents the cost of painting...
Rotated Digits
mediumAn integer x is a good if after rotating each digit individually by 180 degrees, we get a valid number that is different from x.
Sum of Numbers With Units Digit K
mediumGiven two integers num and k, consider a set of positive integers with the following properties: Return the minimum possible size of such a set, or -1 if no such set exists.
Best Time to Buy and Sell Stock III
hardYou are given an array prices where prices[i] is the price of a given stock on the ith day.
Best Time to Buy and Sell Stock IV
hardYou are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.