Project Euler Problems 1 to 100 - Problem 18: Maximum path sum I

Tell us what’s happening:
Describe your issue in detail here.

Neither of the two suggested solutions work :))

Your code so far

-First solution
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];
}


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 = 1; i < triangleCopy.length; i++) {
    triangleCopy[i][0] += triangleCopy[i - 1][0];
    for (let j = 1; j < i; j++) {
      triangleCopy[i][j] += Math.max(triangleCopy[i - 1][j - 1], triangleCopy[i - 1][j]);
    }
    triangleCopy[i][i] += triangleCopy[i - 1][i - 1];
  }
  return Math.max(...triangleCopy[triangleCopy.length - 1]);
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36

Challenge: Project Euler Problems 1 to 100 - Problem 18: Maximum path sum I

Link to the challenge:

They work fine if you don’t delete the testTriangle variable.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.