Triangle
Triangle is a medium dynamic programming problem solved with the 2d dp - max/min of last row pattern.
The best approach, optimal (bottom-up in one array), runs in O(n²) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from top-down recursion up.
Problem
Given a triangle of numbers, find the minimum path sum from top to bottom. From index j in one row you may move to index j or j + 1 in the next row.
Examples
Example 1
- Input
triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]- Output
11- Why
- 2 + 3 + 5 + 1.
Example 2
- Input
triangle = [[-10]]- Output
-10
Constraints
1 <= rows <= 200; row i has i + 1 values.- From index j you can move to j or j + 1 in the next row.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Top-down recursion | O(2ⁿ) | O(n) |
| Optimal (bottom-up in one array) | O(n²) | O(n) |
1Top-down recursion
O(2ⁿ)O(n)best(r, j) = value + min(best(r + 1, j), best(r + 1, j + 1)).
- Base: the last row returns its own value.
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
return best(triangle, 0, 0);
}
private int best(List<List<Integer>> t, int r, int j) {
int v = t.get(r).get(j);
if (r == t.size() - 1) return v;
return v + Math.min(best(t, r + 1, j), best(t, r + 1, j + 1));
}
}class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
return self.best(triangle, 0, 0)
def best(self, t: List[List[int]], r: int, j: int) -> int:
v = t[r][j]
if r == len(t) - 1:
return v
return v + min(self.best(t, r + 1, j), self.best(t, r + 1, j + 1))class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
return best(triangle, 0, 0);
}
private:
int best(vector<vector<int>>& t, int r, int j) {
int v = t[r][j];
if (r == (int)t.size() - 1) return v;
return v + min(best(t, r + 1, j), best(t, r + 1, j + 1));
}
};function minimumTotal(triangle) {
function best(t, r, j) {
const v = t[r][j];
if (r === t.length - 1) return v;
return v + Math.min(best(t, r + 1, j), best(t, r + 1, j + 1));
}
return best(triangle, 0, 0);
}2Optimal (bottom-up in one array)
O(n²)O(n)Copy the last row into dp. For each row above, dp[j] = value + min(dp[j], dp[j + 1]). Reading from the bottom means there is one answer at the top and no need to take a minimum over a row.
- dp = copy of the last row.
- For r from n - 2 down to 0, for j in 0..r: dp[j] = t[r][j] + min(dp[j], dp[j + 1]).
- Return dp[0].
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int n = triangle.size();
int[] dp = new int[n + 1];
for (int r = n - 1; r >= 0; r--)
for (int j = 0; j <= r; j++)
dp[j] = triangle.get(r).get(j) + Math.min(dp[j], dp[j + 1]);
return dp[0];
}
}class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
dp = [0] * (n + 1)
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])
return dp[0]class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
vector<int> dp(n + 1, 0);
for (int r = n - 1; r >= 0; r--)
for (int j = 0; j <= r; j++)
dp[j] = triangle[r][j] + min(dp[j], dp[j + 1]);
return dp[0];
}
};function minimumTotal(triangle) {
const n = triangle.length;
const dp = new Array(n + 1).fill(0);
for (let r = n - 1; r >= 0; r--)
for (let j = 0; j <= r; j++)
dp[j] = triangle[r][j] + Math.min(dp[j], dp[j + 1]);
return dp[0];
}Edge cases to test
- Single row
- Negative numbers
Hints
Hint 1
Work from the bottom row up: every cell becomes its value plus the smaller of its two children. The top cell is the answer.
FAQ
What is the best time complexity for Triangle?
Optimal (bottom-up in one array) runs in O(n²) time and O(n) extra space.
Which pattern does Triangle use?
It is a dynamic programming problem that uses the 2d dp - max/min of last row pattern. Other problems with the same pattern: Minimum Falling Path Sum, Geek's Training.
Is there a brute force solution for Triangle?
Yes. Top-down recursion takes O(2ⁿ) time and O(n) space. best(r, j) = value + min(best(r + 1, j), best(r + 1, j + 1)).
Which edge cases should I test for Triangle?
Single row; Negative numbers.