Triangle

Medium#120from the bottom up

Numbers are stacked in a triangle: one in the first row, two in the second, and so on. Walk from the top to the bottom row; from a cell you may step to either of the two cells right under it. Find the smallest sum of the numbers on such a walk. Our example: [2], [4, 1], [3, 8, 9], [1, 6, 7, 2], the answer is 10.

input
triangle: rows of numbers, the first of one number and each next one a number longer.
returns
The smallest sum of a path from the top to the bottom row. From number j of a row the path goes on to number j or j + 1 of the next row.
constraints
  • 1 ≤ triangle.length ≤ 100
  • triangle[i].length = i + 1
  • −10⁴ ≤ triangle[i][j] ≤ 10⁴
our 12 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} triangle
 * @return {number}
 */
function minimumTotal(triangle) {

}
Ctrl ↵
format
reset

saved in this browser

12 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more dynamic programming (dp) problems

Pattern “Dynamic programming (DP)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.