 All Problems
Triangle
medium
array
dynamic programming
amazon
google

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.

Example 1:

Input:
4
2
3 4
6 5 7
4 1 8 3
Output: 11

(2 → 3 → 5 → 1)

Example 2:

Input:
1
-3
Output: -3

Constraints:

  • 1 ≤ triangle.length ≤ 200
  • triangle[i].length == i + 1
  • -10⁴ ≤ triangle[i][j] ≤ 10⁴

Input format: First line: number of rows n. Next n lines: space-separated integers for each row.

Output format: Minimum path sum.

Run to check your code against the sample cases, or submit to run every case