Problem: given n, find the number of different ways to write n as the sum of 1, 3, 4
◮
Example: for n = 5, the answer is 6 5 = 1+1+1+1+1 = 1+1+3 = 1+3+1 = 3+1+1 = 1+4 = 4+1
1-dimensional DP
6
1-dimensional DP Example
◮
Define subproblems – Let Dn be the number of ways to write n as the sum of 1, 3, 4
◮
Find the recurrence – Consider one possible solution n = x1 + x2 + · · · + xm – If xm = 1, the rest of the terms must sum to n − 1 – Thus, the number of sums that end with xm = 1 is equal to Dn−1 – Take other cases into account (xm = 3, xm = 4)
1-dimensional DP
7
1-dimensional DP Example
◮
Recurrence is then Dn = Dn−1 + Dn−3 + Dn−4
◮
Solve the base cases – D0 = 1 – Dn = 0 for all negative n – Alternatively, can set: D0 = D1 = D2 = 1, and D3 = 2