Solution: Longest Palindromic Subsequence
Understand how to solve the longest palindromic subsequence problem using dynamic programming techniques. Learn brute force recursion, optimize with memoization to reduce time complexity, and apply bottom-up tabular methods for efficient solutions. This lesson equips you with the skills to analyze complexity and implement these algorithms in C++ coding interviews.
Solution #1: Brute Force
By now, you must have noticed a pattern in the way we approach dynamic programming problems.
In this brute force solution,
- If the element at the beginning and the end are the same, we increment our count by two and make a recursive call for the remaining sequence.
- Otherwise, we will skip the element either from the beginning or the end to make two recursive calls for the remaining subsequence. After that, we return the greater result.
Time Complexity
The time complexity of the above algorithm is exponential , where n is the length of the input sequence. The space complexity is which is the maximum number of times that the function is called, so this is the space used to store the recursion stack.
Solution #2: Memoization
As done is memoization, we use an ...