LeetCode: Triangle Solution
Memoized recursionImplementation
1var minimumTotal = function (triangle) {2 const memo = Array.from({ length: triangle.length }, _ =>3 Array(triangle[triangle.length - 1].length).fill(null)4 )56 const recursion = (row, index) => {7 if (memo[row][index] !== null) return memo[row][index]89 if (row + 1 >= triangle.length) return triangle[row][index]1011 return (memo[row][index] =12 triangle[row][index] +13 // "if you are on index i on the current row, you may move to either index i or index i + 1 on the next row"14 Math.min(recursion(row + 1, index), recursion(row + 1, index + 1)))15 }1617 return recursion(0, 0)18}
References
Comments
Loading comments...
Tags
leetcode
recursion
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.