Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Golden Pyramid by jcg
def count_gold(pyramid):
"""
Return max possible sum in a path from top to bottom
"""
# best_row will contain max values for each step
best_row = list(pyramid[0])
for row in range(1, len(pyramid)) :
# no choice for last value
best_row.append(best_row[-1] + pyramid[row][-1])
# for intermediate values, add actual value to max of two valuse in best_row
# reverse loop to modify only "no more to use" values (avoid a tmp reference or list)
for col in range(len(best_row)-2, 0, -1) : # reverse loop. Don't modify!
best_row[col] = pyramid[row][col] + max(best_row[col], best_row[col-1])
# no choice for first value
best_row[0] += pyramid[row][0]
return max(best_row)
#replace this for solution
return 0
April 30, 2014
Comments: