ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def nlis(a):
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)
return sum(C[i] for i in range(n) if L[i] == m)
print(nlis([1, 3, 5, 4, 7]), nlis([2, 2, 2]))- A. 3 3
- B. 2 3
- C. 2 2
- D. 1 3
Correct answer: B. 2 3
For [1, 3, 5, 4, 7] the longest increasing length is 4, reached by 1-3-4-7 and 1-3-5-7, so the count is 2. For [2, 2, 2] strictly increasing runs have length 1 and there are three of them.
More Python Programming questions
- What is printed? python print(bin(29 ^ 15).count('1'))
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def p(n, m): if n == 0: return 1 if n < 0 or m == 0: return…
- What is printed? python import math print(sum(map(int, str(math.factorial(10)))))
- What is printed? python x = 'abc' print(x.islower() and x.isalpha(), ''.isdigit())
- What is printed? python it = iter('ab') print(next(it), list(it), next(it, 'end'))
- What is printed? python print([n for n in range(8) if n and n & (n - 1) == 0])