Triangle - Problem
Given a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
Input & Output
Example 1 — Basic Triangle
$
Input:
triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
›
Output:
11
💡 Note:
The minimum path is 2 → 3 → 5 → 1 with sum = 2 + 3 + 5 + 1 = 11
Example 2 — Small Triangle
$
Input:
triangle = [[-10]]
›
Output:
-10
💡 Note:
Single element triangle, minimum path sum is the element itself
Example 3 — Two Rows
$
Input:
triangle = [[1],[2,3]]
›
Output:
3
💡 Note:
Two possible paths: 1→2 (sum=3) and 1→3 (sum=4). Minimum is 3
Constraints
- 1 ≤ triangle.length ≤ 200
- triangle[0].length == 1
- triangle[i].length == triangle[i - 1].length + 1
- -104 ≤ triangle[i][j] ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code