How to write this simple algorithm?

problem description

1
3 2
4 6 5
7 9 8 10

from top to bottom, you can only take one step on the left (L) or right (R) at a time, and then add up the numbers to find the path with the maximum value and the maximum value.
how to write this algorithm in python?


has an idea to create a two-dimensional array aux , store the maximum value of the current location, and finally only need to return the maximum value of the entire two-dimensional array.

The

path is finally extrapolated using DFS .

for example, in the example of a topic, the aux created ends up like this

[ [ 1 ], 
  [ 4, 3 ],
  [ 8, 10, 8 ],
  [ 15, 19, 18, 18 ]]

the time complexity is O (1 code 2n ^ 2) , n is the length of the array, here is 4.

Menu