## Project Euler 18: Maximum sum from top to bottom of triangular array

#### Project Euler 18 Problem Description

Project Euler 18: By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3

7 5

2 4 6

8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75

95 64

17 47 82

18 35 87 10

20 04 82 47 65

*… {data continues} …*

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

#### Analysis

To solve this problem and problem 67, which is much larger, start the search from the bottom to the top, adding the maximums along the way. This will “bubble” the maximum total to the top of the pyramid.

Let’s follow this technique, step-by-step, with the 4 row triangle example above to show how this works.

### A step-by-step look at this algorithm

3

7 5

2 4 6

8 5 9 3

Starting at the bottom,

We look at 8 and 5, pick the maximum, 8 in this case, and replace the 2 in the previous row with their sum 10.

We look next at 5 and 9, pick the maximum, 9, and replace the 4 in the previous row with their sum 13.

We look lastly at 9 and 3, pick the maximum, 9, and replace the 6 in the previous row with their sum 15.

Now our array looks like:

3

7 5

10 13 15

Let’s do it again. Take the larger of 10 and 13 and add it to 7 making 20.

Take the larger of 13 and 15 and add it to 5 making 20.

Now our array looks like:

3

20 20

At last we take the larger of 20 and 20 (yes, I know they’re the same) and add it to 3 making 23.

And our array looks like:

23

The maximum total path in the triangle.

HackerRank doesn’t add more to the challenge other than running 10 trials. The data for this problem is kept in the second Trinket tab named *pe18.txt*, next to the *main.py* tab.

#### Project Euler 18 Solution

Runs < 0.001 seconds in Python 2.7.Use this link to get the Project Euler 18 Solution Python 2.7 source.

#### Answer

Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose*define*to reveal the answer.

#### Afterthoughts

- The first line of the program reads the data file, pe18.txt, into a two dimensional array named
`table`

. - See also, Project Euler 67 Solution: Find the maximum total from top to bottom in a large triangle of numbers using an efficient algorithm.

Project Euler 18

*Project Euler 18 Solution last updated*

As I understood the formulation of the question, I started coding with starting with the row at index 1 to n -1, adding the maximum of the numbers at the level below at the same index or index + 1 of the current level and then add up a[0][0]. But turns out my answer is wrong.

Why I keep findind 1064, I did it manually too

Hi Murilo, the path is RRLLRLLRRRRRLR. R is down to the right and L is down to the left. Hope this helps you find your problem.

was stuck at this problem. your solution is genius! thanks so much!

Thanks! Always glad to help.

I feel so dumb! Your solution is not only elegant and smart, but the code is compact and to the point. I envy you! Your explanation is the best I found in the web, I really learned a lot!

Thanks Ariel! I’m really glad it helped you.

Great explanation, but shouldn’t the 2nd row of your test triangle be 7 4 instead of 7 5 to coincide with problem 18’s example. Not a big deal at all, just saying..

Thanks Anon, I guess they changed the problem description since 2007. I went to archive.org just to make sure and it used to be (7,5).

http://web.archive.org/web/20071127122922/http://projecteuler.net/index.php?section=problems&id=18

I guess I’ll leave it as it was back in 2007, but I appreciate your keen attention to details!

Nice Solution, It helped me a lot 😛

Nice Solution!

Didn’t understand until I saw your explanation.

Thanks.

Thanks for the acknowledgement. I’m glad it helped you.

This is by far the best explanation I have seen. Thank you!

Thanks Marshall. I’m sorry I missed your kind words until now.