Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple DP solution in Clear category for Golden Pyramid by nickie
def count_gold(pyramid):
"""
A simple dynamic programming approach, O(n^2) time, O(n) space.
"""
for i, row in enumerate(pyramid):
if i == 0:
curr = [row[0]]
else:
prev = curr
def sofar(j, x):
if j == 0:
return prev[j]
elif j == i:
return prev[i-1]
else:
return max(prev[j-1], prev[j])
curr = [x + sofar(j, x) for j, x in enumerate(row)]
return max(curr)
April 30, 2014
Comments: