AlgoFrames

Triangle

Standard · Write code · about 15 min

triangle has one number in row 0, two in row 1, and so on. Start at the top. From index i in one row you may step to index i or i + 1 in the next row.

Return the smallest sum of a path from the top to any cell in the bottom row.

Example 1

Input:

[[[2],[3,4],[6,5,7],[4,1,8,3]]]

Output:

11

Rows are drawn left-aligned, so the two cells below (r, i) are (r + 1, i) and (r + 1, i + 1).

Example 2

Input:

[[[-10]]]

Output:

-10

Constraints

  • 1 ≤ rows ≤ 200
  • row r has r + 1 numbers
  • -100 ≤ triangle[r][i] ≤ 100

Learn the idea first: Grid DP: paths, minimum path sum, obstacles, a visual lesson.

More Grid DP: paths, minimum path sum, obstacles problems