16. Longest Common Increasing Subsequence
β GFG solution to the Longest Common Increasing Subsequence problem: find the length of the longest subsequence that is common to both arrays and strictly increasing using dynamic programming. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given two arrays, a[] and b[], find the length of the Longest Common Increasing Subsequence (LCIS).
An LCIS refers to a subsequence that is present in both arrays and strictly increases. A subsequence is derived by selecting elements from the array in the same order (not necessarily contiguous).
π Examples
Example 1
Input: a[] = [3, 4, 9, 1], b[] = [5, 3, 8, 9, 10, 2, 1]
Output: 2
Explanation: The longest increasing subsequence that is common is [3, 9] and its length is 2.Example 2
Input: a[] = [1, 1, 4, 3], b[] = [1, 1, 3, 4]
Output: 2
Explanation: There are two common subsequences [1, 4] and [1, 3] both of length 2.π Constraints
$1 \le \text{a.size()}, \text{b.size()} \le 10^3$
$1 \le \text{a}[i], \text{b}[i] \le 10^4$
β
My Approach
The optimal approach uses Dynamic Programming with space optimization to efficiently compute the LCIS:
Optimized Dynamic Programming
Initialize DP Array:
Create a
dp[]array of sizen(length of arrayb) initialized to 0.dp[j]represents the length of the longest common increasing subsequence ending with elementb[j].
Iterate Through Array
a:For each element
a[i], maintain a variablecurto track the maximum length of LCIS ending before current position.This
curvariable stores the best LCIS length we can extend if we find a matching element.
Inner Loop Through Array
b:Case 1 - Match Found (
a[i] == b[j]):Update
dp[j] = max(dp[j], cur + 1)because we can extend the previous LCIS by including this common element.
Case 2 - Potential Extension (
b[j] < a[i]):Update
cur = max(cur, dp[j])to track the best LCIS length that can be extended in future matches.
Track the maximum value in
dp[]as the result.
Key Insight:
For each element in
a, we scan throughband:When elements match, we can form a longer LCIS
When
b[j] < a[i], we update our "best so far" for potential extensions
This ensures we maintain the increasing property while finding common elements.
Return Result:
The maximum value encountered in the DP array represents the length of LCIS.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(m Γ n), where m is the size of array
aand n is the size of arrayb. We iterate through each element ofaand for each element, scan through arraybonce.Expected Auxiliary Space Complexity: O(n), as we use a single DP array of size n to store the lengths of LCIS ending at each position in array
b.
π§βπ» Code (C++)
class Solution {
public:
int LCIS(vector<int> &a, vector<int> &b) {
int m = a.size(), n = b.size(), res = 0;
vector<int> dp(n, 0);
for (int i = 0; i < m; i++) {
int cur = 0;
for (int j = 0; j < n; j++) {
if (a[i] == b[j]) {
dp[j] = max(dp[j], cur + 1);
}
if (b[j] < a[i]) {
cur = max(cur, dp[j]);
}
res = max(res, dp[j]);
}
}
return res;
}
};β Code (Java)
class Solution {
public int LCIS(int[] a, int[] b) {
int m = a.length, n = b.length, res = 0;
int[] dp = new int[n];
for (int i = 0; i < m; i++) {
int cur = 0;
for (int j = 0; j < n; j++) {
if (a[i] == b[j]) {
dp[j] = Math.max(dp[j], cur + 1);
}
if (b[j] < a[i]) {
cur = Math.max(cur, dp[j]);
}
res = Math.max(res, dp[j]);
}
}
return res;
}
}π Code (Python)
class Solution:
def LCIS(self, a, b):
m, n = len(a), len(b)
dp = [0] * n
res = 0
for i in range(m):
cur = 0
for j in range(n):
if a[i] == b[j]:
dp[j] = max(dp[j], cur + 1)
if b[j] < a[i]:
cur = max(cur, dp[j])
res = max(res, dp[j])
return resπ§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated