We ❤️ bottom-up dynamic programming!
Notice the relation of how the optimal cost is the value of the house + the optimal value of any path atleast 1 house down (. So if we start from the end and get the base cases of the last two elements, we can continue to define the smaller subproblems and gradually compute the optimal value for our initial problem.
Consider the example
Base Case
For houses and , they cannot jump any more houses so the optimal path from there is the house’s value itself.
For house , its optimal value is its own value + the maximum path from onwards. For example, the house with value 2 can choose the optimal path from .
in this case, so it’ll compute which results in