triangle is a list of rows of numbers. The top row holds one number, and each row below holds one more number than the row above it, so the rows stack into a triangle.
Start at the top number and walk down to the bottom row, one row per step. From position i of a row you may step to position i or position i + 1 of the next row: the two numbers just below you. The cost of a walk is the sum of every number it passes through, the top and the bottom included.
Return the smallest cost any such walk can have.
The cheapest walk is 4 + 7 + 2 + 1 = 14. Stepping to the smaller number each time looks tempting, but the 1 in the second row leads only to 6 or 9, and every walk through it costs at least 4 + 1 + 6 + 8 = 19.
Numbers can be negative. The walk 2 + (-3) + (-8) + 0 = -9 is the cheapest: from the -8 (position 1 of its row) the next step can reach only the 6 or the 0, not the -2 at the far end.
With a single row, the walk starts and ends on the same number.
Constraints
1 <= triangle.length <= 200triangle[0].length == 1triangle[i].length == triangle[i - 1].length + 1-10⁴ <= triangle[i][j] <= 10⁴Think from the bottom up. The cheapest way down from a number is that number plus the cheaper of the cheapest ways down from the two numbers just below it. Always stepping to the smaller neighbour fails: a small number can lead into large ones further down.
14