challenge link
I solved it, but my solution is heavy I guess.
So I am trying to reverse-engineer code from hints page.
First question: how loops work without {}
brackets, I never saw that in curriculum(maybe I forgot but I don’t think so)
Second. When I added some logging like below it started throwing errors. I guess it connected to the first question.
function maximumPathSumI(triangle) {
// Copy the triangle
// Note: not needed if we are ok with mutating the input
const triangleCopy = triangle.map(row => [...row]);
// Find max path
for (let i = triangleCopy.length - 2; i >= 0; i--)
console.log('to check outer loop'); //ADDED by me
console.log(i, triangle[i]); //ADDED by me
for (let j = 0; j <= i; j++)
triangleCopy[i][j] +=
Math.max(triangleCopy[i + 1][j], triangleCopy[i + 1][j + 1])
return triangleCopy[0][0];
}
const testTriangle = [[3, 0, 0, 0],
[7, 4, 0, 0],
[2, 4, 6, 0],
[8, 5, 9, 3]];
maximumPathSumI(testTriangle);