A root path is a path in a triangular lattice which contains exactly one vertex in each row.
What is the maximum sum of the numbers on the vertices of a root path in this lattice of 500 rows?
Details and Assumptions
Explicit example of maximum sum root path, highlighted in red:
Bruteforce is not the best idea because paths exist. An efficient solution can find the answer well under a second.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
An efficient way would be to iterate bottom up.
In my solution, I start from the bottom, find the maximum value of the adjacent cell and then add it up to the cell on the top.
Here's an example. Consider this lattice -
1 2 3 4 5 6 7 8 9 1 0
Let's start working with the lowest row and try to eliminate it. From 4, you can go to 7 or 8. Since 8 is better, we eliminate 7. Since 8 is to largest option, we replace value of 4 with 8 + 4 = 1 2 . From 5, you can do to 8 or 9. Since 9 is better, we eliminate 8 and replace 5 with 9 + 5 = 1 4 . From 6, you can do to 9 or 10. Since 10 is better, we eliminate 9 and replace 6 with 1 0 + 6 = 1 6 .
Now we have got a triangle with a smaller height -
1 2 3 1 2 1 4 1 6
We can continue this process until only 1 row remains.