Pyramids dynamic programming
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Dynamic Programming (DP) is a powerful algorithmic technique used to solve optimization problems. The "Pyramids" problem is one of the classic examples that highlight the elegance and efficiency of dynamic programming. The problem involves finding the maximum path sum in a triangle (or pyramid-shaped structure) where each number along the path must be added to obtain the largest sum from top to bottom.
Problem Description
In this problem, you are given a triangle of numbers. You must start from the top of the triangle and move to an adjacent number on the row immediately below until you reach the base. The challenge is to find the path that yields the maximum sum.
Triangle Example
Consider the following triangle:
Possible paths from the top to the base include:
- 7 → 6 → 3 → 11
- 7 → 6 → 8 → 2
- 7 → 3 → 8 → 10
The task is to calculate the maximum possible sum along these paths.
Dynamic Programming Approach
Key Concept
The dynamic programming approach for solving the pyramids problem involves building a solution bottom-up. Starting from the base of the pyramid, you calculate the maximum sum for each subtree of the pyramid. This effectively transforms the problem into solving smaller subproblems and using those solutions to construct the answer for the larger problem.
Step-by-Step Solution
- Initialize the Base: Begin by initializing the DP table with the values of the last row of the pyramid. These represent the maximum sums if those nodes were the bottom of their paths.
- Build Upwards: Move upwards row by row. For each element in a row, calculate the maximum sum by adding the element to the maximum of the two sums below it.
- Final Result: After processing all rows, the top element of your DP table will hold the maximum path sum from top to bottom.
Example Walkthrough
Given the previous triangle, let's illustrate with dynamic programming:
- Initialize last row:
- Update values above by adding maximum of the two possible values below:
- For third row:
3 + max(11, 2) = 148 + max(2, 10) = 185 + max(10, 9) = 15Resulting DP for second row:
- Continue to second row:
- For second row:
6 + max(14, 18) = 243 + max(18, 15) = 21Resulting DP for first row:
- Finally, calculate for the top of the pyramid:
7 + max(24, 21) = 31
The maximum path sum is 31.
Implementation
Here is a Python implementation of the pyramids dynamic programming solution:
Summary
Below is a summary table highlighting the key points of the dynamic programming approach for the pyramids problem.
| Step | Description |
| Initialize | Start from the last row of the pyramid |
| Build Up | Move up, updating each point with maximum path |
| Result | Top element contains the maximum path sum |
| Time Complexity | , where is the number of rows |
| Space Optimization | In-place update reduces space to |
Conclusion
Dynamic programming offers a methodical approach to solving optimization problems such as the pyramids problem. By breaking down the problem into manageable subproblems and solving each efficiently, we can ensure optimal solutions with improved performance. This example illustrates the strategic thinking required to leverage dynamic programming in algorithm design.
Related reading
- Python - Algorithm find time slots
- python - prefix sum algorithm
- Python - Speed up an A Star Pathfinding Algorithm
- Python - Tree traversal question
- Python - How can I make this code asynchronous?
- Python - Is a dictionary slow to find frequency of each character?
- Python and OpenCV - Improving my lane detection algorithm
- Python Brute Force algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.