Tell us what’s happening:
Describe your issue in detail here.
Again, the deceptive instructions. TOP TO BOTTOM and STARTING AT THE TOP are given. Then the answer is approached from bottom to top. SMFH. Your code so far
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--)
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);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Project Euler Problems 1 to 100 - Problem 18: Maximum path sum I
Maybe a little foreshadowing that these problems were written deceptively with bad instructions could be added to the front of the course since these are granite.
These problems are not deceptive with bad instructions. They are difficult problems that require research and creative thinking to get optimized solutions.
You certainly could have written a top down algorithm to solve this problem.
You can do it. Give it a try and ask questions if you get stuck.
I just verified that it can be done, so long as you are careful. The bottom up approach actually has a key feature that makes it work, and that feature isn’t starting at the bottom.
Tell us what’s happening:
Problem the SECOND with the solution: It doesn’t use “proper” for loop etiquette of curly braces around the for loops… which is something no one is taught to do. If the curly braces are added, the solution doesn’t work. Etiquette says you can leave them off for single line statements, which these aren’t.
YOU HAVE TO REMEMBER, YOU ARE TEACHING, NOT SHOWING OFF. I have encountered this so much in this journey it makes me want to stop. I mean, stack overflow is nearly this every time. Teach each lesson as if it is all they know about programming and coding, not some flex that shows you a trick you learned. The solution should be replaced with the SIMPLEST solution that a beginner could understand. All solutions should be this way. But I don’t run things so we got a bunch of show offs putting people off from coding instead of actually being teachers for newbies.
The solution you wrote doesn’t match the solution posted.
The most recent solution was posted on June 13 and both solutions have used {}s since that day.
The previous version used different whitespace than you posted here. If you use the code as it was originally posted and add {} as the original whitespace indicates, the older version of the solution works fine. The nested loop bodies are single statements.
I do not need your opinions on what I posted. I have experienced this “look what I can do” approach MANY times and as a learning student it is GROWING OLD. I am glad you are doing well.
To you, they are GREAT TEACHERS. When I encounter the above issue, they are FAILING at their task. Your experience may be different.
The above issue you listed is, unfortunately, not accurate. You seem to have made a mistake copying down the answer and are unfortunately blaming us for your transcription error.
I understand you are frustrated. Learning is hard and really taxing. But please remember to be kind to community members who are donating free mentoring and tutoring to you.