2Brute2Best
🔥0

Triangle

Medium·LeetCode #120 Pro OnlyMedium
LeetCode
01

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.

Input

triangle, a list of rows. Row r has r + 1 numbers.

Output

The smallest sum of the numbers on a path from the top row to the bottom row.

Example 1
Input: triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
Output: 11

The path 2 → 3 → 5 → 1 adds up to 11. It is the smallest of all the top-to-bottom paths.

Example 2
Input: triangle = [[-10]]
Output: -10

There is only one number, so the only path is -10.

Constraints
  • • 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?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“from the top to the bottom”
→ A path has one number per row. The rest of a path depends on the rows below, so the table should start from the bottom.
“move to index i or index i + 1”
→ Each cell has two children below it. A cell in the first or last column of its row still has two children, but only one parent above it.
“minimum total”
→ The best total from a cell is its value plus the smaller total of its two children.
03

Approach Complexity Ladder

Brute Force — try both steps from every cellO(2^N)O(N)

best(r, c) = value + the cheaper of best(r+1, c) and best(r+1, c+1), recursing down

Better — memo: store best(r, c) the first timeO(N^2)O(N^2)

The same recursion; each cell's best total is stored in memo[r][c] and reused

Optimal — bottom-up over one listO(N^2)O(N)

Start from the bottom row; dp[c] = row[c] + min(dp[c], dp[c + 1]), going up one row at a time

Brute Force Intuition

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:

dp[r][c] = dp[r−1][c] + dp[r][c−1] (ways) or min(…) + cost (cost).
Remember this · DP — Grid
Trigger
Move only right/down, count paths, minimum path cost, largest square of 1s.
Think
To reach cell (r, c), which cells could I have come from?
Key question
What does dp[r][c] mean, and what are the first row/column?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Grid translate into production architectures and low-level memory mechanics.