{ "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)" ] }, { "cell_type": "markdown", "id": "f06b0164", "metadata": {}, "source": [ "#### Copyright (C) 2025 filifa\n", "\n", "This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)." ] } ], "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 }