Graphical Introduction to Dynamic Programming

Graphical Introduction to Dynamic Programming

That means we can solve this problem in $O(n)$ time, and in $O(1)$ space by computing the subproblems in order of increasing index, storing only the last two values at any given time. The recurrence relation is:

The final answer we want corresponds to $f(n – 1, c)$, where $n$ is the number of denominations we originally were given, and $c$ is the original target value. Because our recurrence relation has two integer inputs, we can lay out our subproblems as a two-dimensional table, with the first input (which denominations we will consider) indexing into the table along the horizontal axis, and the second input (the target value) indexing into the table along the vertical axis.

Source: avikdas.com