Project Euler Problem 18

Hi,
Below is the code I have written in order to solve problem 18.
While it works for smaller test array, it is returning 1064 for the bigger array (1074 is the correct answer)

Path taken by this function for bigger array is (
75
95
47
87
82
75
73
28
83
47
43
73
91
67
98)

function maximumPathSumI(triangle,i=0,j=0) {
  let sum=0
  let temp=[]
  let max=0
  while(i<triangle.length){
    temp=triangle[i].slice(j,j+2)
    max=Math.max(...temp)
    sum+=max
    j=j+temp.indexOf(max)
    i++
    console.log(j,i,max)
  }
  console.log(sum)
  return sum;
}


const testTriangle = [[3, 0, 0, 0],
                    [7, 5, 0, 0],
                    [2, 4, 6, 0],
                    [8, 7, 9, 3]];

console.log(maximumPathSumI(testTriangle));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0

Challenge: Problem 18: Maximum path sum I

Link to the challenge:

Hello. Your algorithm is just checking the next row maximum number, and it isn’t correct because you have to take into consideration the whole path.

1
1     5
1     1     5
100   1     1     5

Your algorithm will go wrong way (1, 5, 5, 5) and will never reach 100

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