Euler project 18 approach
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Project Euler 18 asks for the maximum path sum from top to bottom of a number triangle, moving only to adjacent numbers on the next row. The brute-force approach explores all paths and grows exponentially. The practical solution is dynamic programming, which computes the answer in quadratic time.
Problem Restatement
Given a triangle:
You can move from row r, column c to either r+1,c or r+1,c+1. The goal is the largest possible sum from top to bottom.
For the sample above, best route is 3 -> 7 -> 4 -> 9, total 23.
Why Brute Force Does Not Scale
Each row gives two branching choices, so the number of paths is 2^(n-1) for n rows. That growth is fine for tiny triangles but becomes expensive quickly.
Naive recursion:
This recalculates the same subproblems many times.
Bottom-Up Dynamic Programming
The key insight is local optimal substructure. Best sum at each cell equals cell value plus max of the two best sums directly below it.
Complexity is O(n^2) for n rows because triangle contains about n(n+1)/2 values.
Space-Optimized Variant
You do not need a full DP matrix. One rolling row is enough.
This keeps the same runtime and reduces extra space to O(n).
Recovering the Actual Path
Euler 18 only asks for the max sum, but in real interviews and production analytics you may need the chosen path too.
Path reconstruction adds small bookkeeping overhead and is often worth it for debugging.
Parsing Euler Input Text
Euler datasets are usually provided as multiline text. Parse once and reuse DP function.
Separating parser from solver makes testing easier.
Relation to Euler 67
Project Euler 67 is the same triangle problem with much larger input. A brute-force solution that passes 18 will fail for 67, while bottom-up DP scales naturally.
That is one reason Euler 18 is often treated as an introduction to dynamic programming mindset.
Validation Strategy
Use small known triangles to verify correctness before running full problem input.
Test cases should include:
- one-row triangle
- triangles with ties
- triangles containing negative numbers
- larger random triangles compared against slower reference for small sizes
Negative values are important because greedy local choices can fail badly there.
Common Pitfalls
A common mistake is greedy choice at each step, always taking the larger immediate child. That does not guarantee global optimum.
Another issue is in-place mutation without awareness. If you mutate input triangle and reuse it later, results can become misleading.
Off-by-one errors with c + 1 are also frequent in manual loops. Unit tests on tiny triangles catch this quickly.
Some solutions forget edge cases such as one-row input and crash on indexing.
Finally, developers may optimize too early. Start with clear DP, then apply space optimization only if needed.
Summary
- Euler 18 is a maximum path sum problem best solved with dynamic programming.
- Bottom-up DP gives
O(n^2)time and simple implementation. - Space can be reduced to
O(n)with a rolling-row approach. - Path reconstruction is easy when you store child choices.
- The same approach scales directly to larger variants such as Euler 67.

