Project Euler Problem 18

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top

Project Euler Problem 18

Solution

Answer: 1074

This is a classic dynamic programming problem.

At each position in the triangle, the maximum sum reaching that position depends only on the two possible parents above it. Instead of checking all $2^{14} = 16384$ routes, we propagate the best possible total row by row.

Let $dp[i][j]$ be the maximum total reaching element $j$ in row $i$.

Transition:

$$dp[i][j] = \text{triangle}[i][j] + \max(dp[i-1][j-1], dp[i-1][j])$$

with edge cases on the left and right borders.

A more efficient approach is to work bottom-up:

  • Start from the second-last row.
  • Replace each number with:

$$\text{value} + \max(\text{two children below})$$

  • After collapsing all rows, the top element becomes the maximum total.

Using the triangle from the problem:

triangle = [
    [75],
    [95, 64],
    [17, 47, 82],
    [18, 35, 87, 10],
    [20, 4, 82, 47, 65],
    [19, 1, 23, 75, 3, 34],
    [88, 2, 77, 73, 7, 63, 67],
    [99, 65, 4, 28, 6, 16, 70, 92],
    [41, 41, 26, 56, 83, 40, 80, 70, 33],
    [41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
    [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
    [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
    [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
    [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
    [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]
]

for row in range(len(triangle) - 2, -1, -1):
    for col in range(len(triangle[row])):
        triangle[row][col] += max(
            triangle[row + 1][col],
            triangle[row + 1][col + 1]
        )

print(triangle[0][0])

Tracing the reductions upward yields the final top value:

$$1074$$

Answer: 1074