• Asked to answer
    + 1 comment
    Endorsed by decuqa

    This is a good observation. I spent a lot of time solving this question (I am a beginner). I personally don't approach a problem in the view of solving it by DP, but spend some time just thinking about the solution. Also the presence of recurrence relations gives an indication of solving it by recursion and optimizing the code by using DP.

    In this case I would suggest you to actually look at the problem in a mathematical way. Given the hint that this problem is categorized under DP, try to find a recurrence solution. And then write the code. I found a conceptually more obvious solution which is also O(N). I hope even you get it.