add problem 67
This commit is contained in:
84
notebooks/problem0067.ipynb
Normal file
84
notebooks/problem0067.ipynb
Normal file
@@ -0,0 +1,84 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "35b56ed4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# [Maximum Path Sum II](https://projecteuler.net/problem=67)\n",
|
||||
"\n",
|
||||
"Since we took the time to find an efficient method in [problem 18](https://projecteuler.net/problem=18), we can just reuse that method here.\n",
|
||||
"\n",
|
||||
"So just read the triangle into a list-of-lists."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "194eef78",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"txt/0067_triangle.txt\") as f:\n",
|
||||
" triangle = [[int(n) for n in line.split(' ')] for line in f]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b39b979e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Then we'll use the bottom-up method from before (you could also use the top-down method)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "fa8a353f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"7273"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def max_path_sum(tri):\n",
|
||||
" for i in reversed(range(0, len(tri) - 1)):\n",
|
||||
" for j in range(0, len(tri[i])):\n",
|
||||
" tri[i][j] += max(tri[i+1][j], tri[i+1][j+1])\n",
|
||||
" \n",
|
||||
" return tri[0][0]\n",
|
||||
"\n",
|
||||
"max_path_sum(triangle)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "SageMath 9.5",
|
||||
"language": "sage",
|
||||
"name": "sagemath"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user