Triangle
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
jof a row the path goes on to numberjorj + 1of the next row. - constraints
- 1 ≤ triangle.length ≤ 100
- triangle[i].length = i + 1
- −10⁴ ≤ triangle[i][j] ≤ 10⁴
- 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
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more dynamic programming (dp) problems
- 70Climbing StairsEasysolve
- 121Best Time to Buy and Sell StockEasysolve
- 198House RobberMediumsolve
- 322Coin ChangeMediumsolve
- 300Longest Increasing SubsequenceMediumsolve
- 62Unique PathsMediumsolve
- 1143Longest Common SubsequenceMediumsolve
- 53Maximum SubarrayMediumsolve
- 918Maximum Sum Circular SubarrayMediumsolve
- 139Word BreakMediumsolve
- 64Minimum Path SumMediumsolve
- 63Unique Paths IIMediumsolve
- 97Interleaving StringMediumsolve
- 72Edit DistanceMediumsolve
- 221Maximal SquareMediumsolve
- 123Best Time to Buy and Sell Stock IIIHardsolve
- 188Best Time to Buy and Sell Stock IVHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.