// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (26 votes, average: 5.00 out of 5)
Loading...

Project Euler Solutions

Project Euler 18 Solution

Project Euler 18 Solution

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
This program and method
solves all test cases for
Project Euler 18 on HackerRank

Project Euler 18 Solution

Runs < 0.001 seconds in Python 2.7.
download arrowUse 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.
|1074|

Afterthoughts

Project Euler 18

Project Euler 18 Solution last updated

Discussion

15 Responses to “Project Euler 18 Solution”

  1. 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.

    Posted by Adi | January 18, 2015, 5:44 PM
  2. Why I keep findind 1064, I did it manually too

    Posted by Murilo Camargos | January 27, 2013, 9:21 AM
  3. was stuck at this problem. your solution is genius! thanks so much!

    Posted by Raj | July 9, 2011, 8:54 AM
  4. 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!

    Posted by Ariel Meilij | January 26, 2011, 8:08 PM
  5. 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..

    Posted by Anonymous | January 17, 2011, 9:10 PM
  6. Nice Solution, It helped me a lot 😛

    Posted by S2xC | July 11, 2010, 11:50 PM
  7. Nice Solution!

    Posted by Amir | June 19, 2010, 8:57 AM
  8. Didn’t understand until I saw your explanation.

    Thanks.

    Posted by tom | September 7, 2009, 11:27 PM
  9. This is by far the best explanation I have seen. Thank you!

    Posted by Marshall | August 18, 2009, 10:51 AM

Post a comment