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.

The triangle is represented as a 2D array where triangle[row][col] gives you the number at that position.

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 = 11. Path goes from top (2) → left (3) → right (5) → right (1).
Example 2 — Single Element
$ Input: triangle = [[-10]]
Output: -10
💡 Note: Triangle with only one element, so the minimum path sum is just that element: -10.
Example 3 — Negative Numbers
$ Input: triangle = [[-1],[2,3],[1,-1,-3]]
Output: -1
💡 Note: Optimal path: -1 + 2 + (-3) = -2, but better path is -1 + 3 + (-3) = -1.

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
Triangle: Find Minimum Path Sum2346574183Optimal Path: 2 → 3 → 5 → 1Minimum Sum: 2 + 3 + 5 + 1 = 11
Understanding the Visualization
1
Input Triangle
2D array representing triangle with path costs
2
Find Paths
From each position, can move to adjacent positions below
3
Minimum Sum
Return the smallest sum among all possible paths
Key Takeaway
🎯 Key Insight: Use bottom-up DP to build minimum path sums, with space optimization by modifying input in-place
Asked in
Amazon 45 Microsoft 38 Google 32 Facebook 28
78.5K Views
High Frequency
~25 min Avg. Time
2.9K 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