-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem0082.py
39 lines (25 loc) · 890 Bytes
/
problem0082.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#Project Euler Problem 82: Path sum: Three ways
import time
import numpy as np
tic = time.time()
with open("0082_matrix.txt") as f:
lines = f.readlines()
matrix = np.array([[int(x) for x in line.split(",")] for line in lines])
rows, cols = matrix.shape
T = np.zeros((rows, cols), dtype=int)
#init the first column
T[:, 0] = matrix[:, 0]
for j in range(1, cols):
#move right
T[:, j] = T[:, j - 1] + matrix[:, j]
for i in range(1, rows):
#move down
T[i, j] = min(T[i, j], T[i - 1, j] + matrix[i, j])
for i in range(rows - 2, -1, -1):
#move up
T[i, j] = min(T[i, j], T[i + 1, j] + matrix[i, j])
#minimum of the last column as we can finish in any cell in the last column
min_path_sum = min(T[i, cols - 1] for i in range(rows))
print(min_path_sum)
tac = time.time()
print("Elapsed Time: %.2f seconds" % (tac - tic))