Triangle
Problem Statement
You are given a triangle of numbers. Row 0 has one number, row 1 has two, and so on down to the last row. Start at the top and walk down to the bottom row. Each step goes to one of the two numbers directly below: the one at the same index, or the one at index + 1 in the next row.
Return the smallest total of a path from the top to the bottom.
triangle, a list of rows. Row r has r + 1 numbers.
The smallest sum of the numbers on a path from the top row to the bottom row.
The path 2 → 3 → 5 → 1 adds up to 11. It is the smallest of all the top-to-bottom paths.
There is only one number, so the only path is -10.
- • 1 ≤ number of rows ≤ 200
- • Row 0 has one number, and each row has one more number than the row above.
- • −10^4 ≤ each number ≤ 10^4
- • Follow-up: can you solve it with only O(n) extra space, where n is the number of rows?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
best(r, c) = value + the cheaper of best(r+1, c) and best(r+1, c+1), recursing down
The same recursion; each cell's best total is stored in memo[r][c] and reused
Start from the bottom row; dp[c] = row[c] + min(dp[c], dp[c + 1]), going up one row at a time
Let best(r, c) be the smallest total of a path from (r, c) down to the bottom row. Each cell has two children below it, (r+1, c) and (r+1, c+1), so best(r, c) = triangle[r][c] + min(best(r+1, c), best(r+1, c+1)). On the bottom row, best is just the number.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Grid translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.