Last active
August 26, 2020 15:02
-
-
Save noel-yap/392c64ba29e645827c03dba97c43ac73 to your computer and use it in GitHub Desktop.
Maximum path sum II: https://projecteuler.net/problem=67
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from typing import TextIO | |
try: | |
triangle: TextIO = open('triangle.txt') | |
tree = [[int(s) for s in line.split()] | |
for line in [line.rstrip() for line in triangle.readlines()]] | |
for r in range(len(tree) - 1, 0, -1): | |
for c in range(0, r): | |
if tree[r][c] > tree[r][c + 1]: | |
tree[r - 1][c] += tree[r][c] | |
else: | |
tree[r - 1][c] += tree[r][c + 1] | |
print(tree[0][0]) | |
finally: | |
triangle.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment