Problems - Set 1
1. Longest Common Subsequence (LCS)
Given two strings A
and B
, find the length of the longest subsequence that is present in both strings.
Example:
Input: A = "abcde"
, B = "ace"
Output: 3
Explanation: The longest common subsequence is "ace".
Input: A = ""
, B = "ace"
Output: 0
Explanation: The longest common subsequence of empty and non-empty string is 0.
Tabulation Approach:
Create a 2D DP table,
dp[m+1][n+1]
, where m and n are the lengths oftext1
andtext2
.dp[i][j]
represents the length of the LCS of the firsti
characters oftext1
and the firstj
characters oftext2
.Fill the DP table iteratively:
If
text1[i-1] == text2[j-1]
, thendp[i][j] = dp[i-1][j-1] + 1
.Otherwise,
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
.
The result is stored in
dp[m][n]
.
Recursive Approach
Compare the characters from the end of both strings.
If the characters match, the result is
1 + LCS of remaining strings
.If the characters don't match, the result is the maximum of:
LCS of the first string with the second string reduced by one character.
LCS of the second string with the first string reduced by one character.
Base Case: If either string is empty, the LCS is
0
.
Time Complexity
Recursive: O(2^max(m,n)), exponential due to overlapping subproblems. m and n are the lengths of the strings.
Tabulation: O(m×n): Two nested loops iterate through the strings.
Space Complexity: O(m×n): DP table size.
2. Knapsack Problem (0/1)
Given n
items, each with a weight w[i] and a value v[i], and a knapsack with a maximum capacity W, determine the maximum value that can be achieved by selecting a subset of items, such that the total weight does not exceed W. You can either include an item (1) or exclude it (0).
Example
Items: weights=[1,2,3]
, values=[6,10,12]
Knapsack Capacity: 5
Output: Maximum Value = 22
Explanation: Include items with weights 2
and 3
(values 10
and 12
).
Example
Items: Weights = [1, 3, 4, 5]
, Values: [1, 4, 5, 7]
Capacity: 7
Output: 9 Explanation: Pick items with weights 3 and 4.
Recursive Approach
Choices:
If the weight of the current item is greater than the remaining capacity, skip the item.
Otherwise, choose between:
Including the item (add its value and reduce capacity by its weight).
Excluding the item.
Take the maximum value of these choices.
Base Case:
If no items are left or the capacity becomes 0, return 0.
Tabulation Method (Bottom-Up DP)
DP Table Definition:
Use a 2D DP table,
dp[n+1][capacity+1]
.dp[i][j]
represents the maximum value achievable with the firsti
items and capacityj
.
Transition:
If the current item's weight weights[i-1] exceeds j, exclude it: dp[i][j]=dp[i−1][j]
Otherwise, choose the maximum value between excluding or including the item: dp[i][j]=max( dp[i−1][j], values[i-1] + dp[i−1][j−weights[i-1]] )
Result:
The maximum value is stored in dp[n][capacity].
Time Complexity
Recursive: O(2^n), exponential due to overlapping subproblems.
Tabulation: O(n×capacity).
3. Longest Palindromic Subsequence
Given a string s, find the length of its longest palindromic subsequence. A subsequence is a sequence derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example
Input: String s="bbbab"
Output: Longest Palindromic Subsequence Length = 4 Explanation: The LPS is "bbbb".
Recursive Approach
Key Idea:
If the first and last characters match:
The result is 2+LPS(s,l+1,r−1) (include both characters and find LPS for the inner substring).
Otherwise:
Take the maximum of:
Excluding the first character: LPS(s,l+1,r).
Excluding the last character: LPS(s,l,r−1).
Base Case:
If l>r: Return 0 (invalid substring).
If l==r: Return 1 (a single character is a palindrome).
Tabulation Method (Bottom-Up DP)
DP Table Definition:
Let dp[i][j] represent the length of the LPS in the substring s[i....j].
Initialize dp[i][i]=1 (single-character palindromes).
Transition:
If s[i]==s[j]: dp[i][j]=2+dp[i+1][j−1]
Otherwise: dp[i][j]=max(dp[i+1][j],dp[i][j−1])
Result:
dp[0][n−1] contains the length of the LPS for the entire string.
Time Complexity
Recursive: O(2^n), exponential due to overlapping subproblems.
Tabulation: O(n^2), for the nested loops.
4. Coin Change Problem
Given a set of coin denominations coins[] and a target amount target, determine the minimum number of coins required to make up the target amount. If it is not possible to make the amount, return -1.
Example
Input: Coins = [1,2,5] Target = 11
Output: Minimum Coins Required = 3 Explanation: The combination of coins is 5,5,1 or 1,1,1,2,2,2,2 (minimum is 3 coins).
Recursive Approach
Key Idea:
For each coin in coins[], reduce the target amount by the coin's value and recursively solve for the remaining amount.
If the target becomes zero, the minimum number of coins required is zero for that base case.
If the target becomes negative, it means this path is invalid.
Recursive Formula:
For target: minCoins(target)=min(minCoins(target−coins[i]))+1
Base Case:
target=0: Return 0 (no coins needed).
target<0: Return ∞ (invalid case).
Tabulation Method (Bottom-Up DP)
DP Table Definition:
Let dp[i] represent the minimum number of coins required to make up the amount i.
Initialize dp[0]=0 (0 coins needed for amount 0).
Transition:
For each coin in coins[]:
Update dp[i] as: dp[i]=min(dp[i],dp[i−coin]+1)
Result:
If dp[target] is ∞, return -1 (not possible).
Otherwise, return dp[target].
Time and Space Complexity
Recursive Approach:
Time Complexity: O(coins^n), where n is the target amount.
Space Complexity: O(n), for the recursion stack.
Tabulation:
Time Complexity: O(coins×target), as we iterate through coins and the target.
Space Complexity: O(target), for the DP array.
5. Unique Paths
You are given a m×n grid. You are standing at the top-left corner of the grid (cell (0,0), and you want to reach the bottom-right corner (cell (m−1,n−1)). You can only move either right or down at any point. Find the total number of unique paths to reach the destination.
6. Minimum Path Sum
7. Subset Sum Problem
Last updated
Was this helpful?