Longest common increasing subsequence (LCS + LIS) (Tabulation)

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.

Hint

Building upon understanding of Longest common increasing subsequence (LCS + LIS) (Memoization). Tabulation essentially reconstructs the cache.

Start with the foundation – the base cases, which are the simplest solutions. Then, layer by layer, use the results of the previous layers to calculate the solutions for the next level. This way, we systematically build up the entire solution, avoiding redundant calculations and ensuring efficiency. It is like building a pyramid.

The underlying calculation logic remains fundamentally identical to the original recursive approach, albeit executed in reverse order.

# Python implementation
def count(a, b):
  cache = [0] * len(b)

  for i in range(len(a)):
    mx = 0

    for j in range(len(b)):
      if b[j] < a[i]:
        mx = max(cache[j], mx)

      if b[j] == a[i]:
        cache[j] = max(cache[j], mx + 1)

  return max(cache)

a = [3, 4, 9, 1]
b = [5, 3, 8, 9, 10, 2, 1]

print(count(a, b))
// Javascript implementation
function count(a, b) {
  const cache = Array(b.length).fill(0);

  for (let i = 0; i < a.length; i++) {
    let max = 0;

    for (let j = 0; j < b.length; j++) {
      if (b[j] < a[i]) {
        max = Math.max(cache[j], max);
      }

      if (b[j] == a[i]) {
        cache[j] = Math.max(cache[j], max + 1);
      }
    }
  }

  return Math.max(...cache);
}

const a = [3, 4, 9, 1];
const b = [5, 3, 8, 9, 10, 2, 1];

console.log(count(a, b));