120. Triangle
My accepted JavaScript solution to LeetCode problem 120, Triangle, running in 2ms.
- Difficulty: Medium
- JavaScript
- Runtime 2ms
- Memory 52.9MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 2ms, memory 52.9MB, accepted 2025-12-24.
/**
* @param {number[][]} triangle
* @return {number}
*/
var minimumTotal = function(triangle) {
const n = triangle.length;
// Use bottom-up DP with O(n) space
const dp = [...triangle[n - 1]];
for (let row = n - 2; row >= 0; row--) {
for (let col = 0; col <= row; col++) {
dp[col] = triangle[row][col] + Math.min(dp[col], dp[col + 1]);
}
}
return dp[0];
};