We notice that the solution is based on the previous 2 subproblems. This is a bottom-up dynamic programming approach where
The intuition for this solution being fibonacci’s sequence is that for , if we consider , we’ll see that we can get from by taking 1 step. For , we can take 2 steps and get to . So we can see the optimal solution is recursively related to its smaller subproblems.
Note there is a way to not use extra memory in terms swapping variables but we will stick with this approach as it clearly explains the recurrence relation!