Given two arrays, a[] and b[], find the length of the longest common increasing subsequence(LCIS). LCIS refers to a subsequence that is present in both arrays and strictly increases.
This algorithm identifies the Longest Common Subsequence (LCS) within two given arrays. It operates by systematically comparing elements between the arrays. Begin by examining the first elements of both arrays. At each comparison, three possible actions arise:
The algorithm iteratively evaluates these choices, selecting the path that ultimately results in the longest common subsequence. To ensure the subsequence is strictly increasing, the algorithm maintains a "last" variable. This variable stores the last element included in the LCS, preventing the inclusion of any smaller elements that would violate the increasing order.
def count(a, b, last): if len(a) <= 0 or len(b) <= 0: return 0 result = 0 if a[0] == b[0]: if last < a[0]: result = 1 + count(a[1:], b[1:], a[0]) return max(result, count(a[1:], b, last), count(a, b[1:], last)) a = [3, 4, 9, 1] b = [5, 3, 8, 9, 10, 2, 1] print(count(a, b, -1))
function count(a, b, last) { if (a.length <= 0 || b.length <= 0) { return 0; } let result = 0; if (a[0] === b[0]) { if (last < a[0]) { result = 1 + count(a.slice(1), b.slice(1), a[0]); } } return Math.max(result, count(a.slice(1), b, last), count(a, b.slice(1), last)); } const a = [3, 4, 9, 1]; const b = [5, 3, 8, 9, 10, 2, 1]; console.log(count(a, b, -1));