Project Euler Problem 81 Solution

Project Euler Problem 81 Solution

Path Sum: Two Ways

by {BetaProjects} | Project Euler & HackerRank

Project Euler Problem 81 Statement

In the $5$ by $5$ matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to $2427$.

$$ \begin{pmatrix} \color{darkred}{131} & 673 & 234 & 103 & 18\\ \color{darkred}{201} & \color{darkred}{96} & \color{darkred}{342} & 965 & 150\\ 630 & 803 & \color{darkred}{746} & \color{darkred}{422} & 111\\ 537 & 699 & 497 & \color{darkred}{121} & 956\\ 805 & 732 & 524 & \color{darkred}{37} & \color{darkred}{331} \end{pmatrix} $$

Find the minimal path sum from the top left to the bottom right by only moving right and down in matrix.txt (right click and "Save Link/Target As..."), a 31K text file containing an $80$ by $80$ matrix.

Solution

This solution uses dynamic programming, a technique for breaking down complex problems into smaller, overlapping subproblems. It's similar to the approach used to solve Project Euler problem 18. You can find a helpful explanation of dynamic programming on that page.

We begin by reading N (matrix size) for the range parameter and then N lines of space-separated numbers to create the matrix, M.

This dynamic programming approach modifies the matrix in-place, where each cell eventually contains the minimum path sum to reach that cell. For each position (i,j), it adds to its current value the minimum of: The value from above (M[i-1][j]) or the value from the left (M[i][j-1])

There are some key details to explain before we give an example:

Clairifying example with a small matrix:

Initial matrix:
[   3    8    4    2    7 ]
[   9    2    6   15    4 ]
[  12    5   18    3    8 ]
[   4   17    9   14    2 ]
[  11    6    7    5   13 ]

After processing (showing minimum path sums to reach each position):
[   3   11   15   17   24 ]
[  12   13   19   32   28 ]
[  24   18   36   35   36 ]
[  28   35   44   49   38 ]
[  39   41   48   53   51 ]

The final answer is 51, representing the minimum path sum from top-left to bottom-right.

Let's look at some example calculations:

Position [0,1]: 8 + 3 = 11 (can only come from left)
Position [1,0]: 9 + 3 = 11 (can only come from above)
Position [1,1]: 2 + min(11, 12) = 2 + 11 = 13 (takes shorter path from above)
Position [2,2]: 18 + min(19, 18) = 18 + 18 = 36 (takes shorter path from left)

This smaller example makes it easier to verify the calculations by hand.

HackerRank version

HackerRank Project Euler 81 the matrix size is increased to 1000 × 1000.

Python Source Code

M = [list(map(int,input().split())) for _ in range(int(input()))]
for i in range(len(M)):
    for j in range(len(M)):
        if i+j:
            M[i][j]+= min(
                M[i-1][j] if i else float('inf'), M[i][j-1] if j else float('inf') )
print(M[-1][-1])	

Last Word