You are required to find the greatest path sum starting at t
You are required to find the greatest path sum starting at the top of the triangle and moving only to adjacent numbers on the row below.
In the above triangle, the maximum path sum is 3 + 7 + 4 + 9 = 23
Please write in Python, if possible can someone explain the logic?
Solution
def printTriangle(n):
list_of_nums = []
for i in range(1,n+1):
print(\' \'.join(str(i)*i))
list_of_nums.append(str(i)*i)
return list_of_nums
def sum(l1):
sum_v = 0
for i in range(1,len(l1)+1):
if i == 1:
sum_v += int(l1[i-1])
else:
try:
sum_v += int(l1[i-1][i-1])
except Exception as e:
break
return sum_v
print(sum(printTriangle(5)))
