Data Structures & AlgorithmsVery hardICPC regional · DP counting
Problem: How many longest increasing subsequences does [1, 3, 5, 4, 7] have? Run the DP that counts both length and number of optimal subsequences.
Problem: How many longest increasing subsequences does [1, 3, 5, 4, 7] have? Run the DP that counts both length and number of optimal subsequences.
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] = L[j] + 1
C[i] = C[j]
elif L[j] + 1 == L[i]:
C[i] += C[j]
m = max(L)
print(sum(C[i] for i in range(n) if L[i] == m))What is printed?- A. 1
- B. 3
- C. 2
- D. 4
Correct answer: C. 2
The length is 4, achieved by 1,3,5,7 and 1,3,4,7. Ties in length must be summed, not overwritten.
More Coding Interview Problems questions
- Problem: What is the largest number of distinct characters in any window of length 3 of 'abcabc'? Run the code. python s = 'abcabc'…
- Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code. python from itertools…
- Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order. python a = [5, 1…
- Problem: What is the maximum sum of non-adjacent elements of [3, 2, 7, 10]? Run the DP. python a = [3, 2, 7, 10] inc = exc = 0 for x in a…
- Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code. python f = [1, 1] for in range(3)…
- Problem: How many trailing zero bits does 40 have in binary? Run the code. python print((40 & -40).bitlength() - 1) What is printed?