Triangle
Triangle is LeetCode 120 (Medium). triangle has n rows, and row i holds i + 1 numbers.
- Start at the single number on top and move down one row at a time.
- From index
jyou may step to indexjorj + 1on the row below. - Return the smallest possible sum of a path that reaches the bottom row.
Values can be negative. The follow-up asks for only O(n) extra space, where n is the number of rows.
- 1 <= triangle.length <= 200
- triangle[0].length == 1
- triangle[i].length == triangle[i - 1].length + 1
- -10⁴ <= triangle[i][j] <= 10⁴
Intuition
Choosing from the top is hard, because a small step now can lead into large numbers later. Choosing from the bottom is easy. A number on the last row has nothing below it, so its best path is just itself.
One row up, each number has exactly two children, and both children already know their best path to the bottom. So its own best is its value plus the smaller of its two children's bests. Repeat row by row, and the top cell ends up holding the answer.
Each row only reads the row directly below it, so a single array of length n is enough.
"Move down to one of two neighbours, minimise the total" is a grid DP where every cell depends on a fixed set of cells below it. Solving from the side with no choices left, here the bottom row, removes all the boundary checks.
Approach
Before reading on, decide what the best path from a bottom-row cell is, and how one cell can reuse the answers of its two children. Then check whether you need a full 2D table or only one row. Aim for O(n²) time and O(n) space.
Copy the bottom row
Set dp to a copy of the last row. dp[j] means the smallest sum from cell j of the current row down to the bottom, and for the last row that is the cell's own value.
Fold each row into the one below
For rows r = n - 2 up to 0, and j = 0 to r, set dp[j] = triangle[r][j] + min(dp[j], dp[j + 1]). The two children of cell (r, j) are (r + 1, j) and (r + 1, j + 1), and their bests are still sitting in dp[j] and dp[j + 1].
Why left to right is safe
Writing dp[j] overwrites a child value, but only cell j itself ever reads dp[j] again: cell j + 1 reads dp[j + 1] and dp[j + 2]. So no later cell on the row loses a value it needs.
Return the top
After row 0 is folded, dp[0] is the best path from the top cell to the bottom, which is the answer. The rest of dp holds stale values from lower rows and is ignored.
Code in Python, C++ and Java
Common pitfalls
Taking the smaller step on the way down
j, total = 0, triangle[0][0]
for r in range(1, n):
if triangle[r][j + 1] < triangle[r][j]:
j += 1
total += triangle[r][j]dp[j] = triangle[r][j] + min(dp[j], dp[j + 1])
A greedy step only sees the next row. On [[1], [2, 3], [9, 9, 1]] it steps to 2 and then must take a 9, for 12, while 1 → 3 → 1 sums to 5.
Reading past the end of a zero-filled row
dp = [0] * n
for r in range(n - 1, -1, -1):
for j in range(r + 1):
dp[j] = triangle[r][j] + min(dp[j], dp[j + 1])dp = [0] * (n + 1)
for r in range(n - 1, -1, -1):
...Starting from zeros means the bottom row is folded too, and its last cell reads dp[n]. With length n that index does not exist, so even [[-10]] crashes. Either pad to n + 1 or copy the bottom row and start one row higher.
Edge cases
The loop over rows never runs, so the answer is that one value, even when it is negative: [[-10]] returns -10.
Complexity
dp, one entry per row.Triangle FAQ
Can I solve it top-down instead?
Yes. Let best[r][j] be the smallest sum from the top down to (r, j); it takes from (r - 1, j - 1) and (r - 1, j), but the first and last cell of each row have only one parent. The answer is then the minimum of the whole last row. Bottom-up avoids both the special cases and the final scan.
Is it fine to write the sums into the triangle itself?
It works and drops the extra memory to O(1): fold each row into triangle[r + 1] and return triangle[0][0]. The catch is that it destroys the input, which a caller may still need, so most interviewers expect you to ask first.