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
INPUTALGORITHMRESULT234657Triangle ArrayMove to adjacentpositions below1Start from bottom2Work upward3Add min of below4Get top resultBottom-Up DPO(n²) time, O(1) space11Minimum SumPath: 2→3→5→1Sum = 2+3+5+1Optimal PathThrough triangleKey Insight:Each position depends only on two adjacent positions below - perfect for bottom-up DPTutorialsPoint - Triangle | Space-Optimized Dynamic Programming
Asked in
Amazon 15 Apple 8 Microsoft 6 Google 4
23.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen