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]]))
  1. A. 19
  2. B. 23
  3. C. 18
  4. 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

Exams that ask this topic