ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def tri(t):
dp = t[-1][:]
for row in range(len(t) - 2, -1, -1):
for j in range(len(t[row])):
dp[j] = t[row][j] + max(dp[j], dp[j + 1])
return dp[0]
print(tri([[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]))- A. 19
- B. 23
- C. 18
- D. 21
Correct answer: D. 21
The DP runs bottom-up, and each cell takes its own value plus the larger of the two cells below. The best path 2, 4, 7, 8 totals 21.
More Python Programming questions
- What is printed? python def lps(s): n = len(s) dp = [[0] n for in range(n)] for i in range(n): dp[i][i] = 1 for L in range(2, n + 1): for…
- What is printed? python fs = [lambda: i for i in range(3)] gs = [lambda i=i: i for i in range(3)] print([f() for f in fs], [g() for g in…
- What is printed? python def isprime(n): if n < 2: return False i = 2 while i i <= n: if n % i == 0: break i += 1 else: return True return…
- For a Python list a, which operation takes time proportional to j - i?
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def f(n, last): if n == 0: return 1 total = f(n - 1, 0) if…
- What is printed? python a = [1, 2, 4] cnt = 0 for mask in range(1 << len(a)): s = sum(a[i] for i in range(len(a)) if mask >> i & 1) if s %…