Data Structures & AlgorithmsHardProduct Company SDE pattern

What is printed? (LIS length with O(n²) DP and the number of LIS)

What is printed? (LIS length with O(n²) DP and the number of LIS)
a = [1, 3, 5, 4, 7]
n = len(a); L = [1] * n; C = [1] * n
for i in range(n):
    for j in range(i):
        if a[j] < a[i]:
            if L[j] + 1 > L[i]: L[i], C[i] = L[j] + 1, C[j]
            elif L[j] + 1 == L[i]: C[i] += C[j]
m = max(L)
print(m, sum(c for l, c in zip(L, C) if l == m))
  1. A. 4 2
  2. B. 4 1
  3. C. 3 2
  4. D. 5 2

Correct answer: A. 4 2

Track both the best length ending at i and how many subsequences achieve it. 1, 3, 5, 7 and 1, 3, 4, 7 both have length 4 → 2. O(n²).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic