"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Solution Notebook"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Problem: Determine the total number of unique ways to make n cents, given coins of denominations less than n cents.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Constraints\n",
"\n",
"* Do the coins have to reach exactly n cents?\n",
" * Yes\n",
"* Can we assume we have an infinite number of coins to make n cents?\n",
" * Yes\n",
"* Do we need to report the combination(s) of coins that represent the minimum?\n",
" * No\n",
"* Can we assume the coin denominations are given in sorted order?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Test Cases\n",
"\n",
"* coins: None or n: None -> Exception\n",
"* coins: [] or n: 0 -> 0\n",
"* coins: [1, 2, 3], n: 5 -> 5"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"We'll use a bottom-up dynamic programming approach.\n",
"\n",
"<pre>\n",
"The rows (i) represent the coin values.\n",
"The columns (j) represent the totals.\n",
"\n",
" -------------------------\n",
" | 0 | 1 | 2 | 3 | 4 | 5 |\n",
" -------------------------\n",
"0 | 1 | 0 | 0 | 0 | 0 | 0 |\n",
"1 | 1 | 1 | 1 | 1 | 1 | 1 |\n",
"2 | 1 | 1 | 2 | 2 | 3 | 3 |\n",
"3 | 1 | 1 | 2 | 3 | 4 | 5 |\n",
" -------------------------\n",
"\n",
"Number of ways to get total n with coin[n] equals:\n",
"* Number of ways to get total n with coin[n - 1] plus\n",
"* Number of ways to get total n - coin[n]\n",
"\n",
"if j == 0:\n",
" T[i][j] = 1\n",
"if row == 0:\n",
" T[i][j] = 0\n",
"if coins[i] >= j\n",
" T[i][j] = T[i - 1][j] + T[i][j - coins[i]]\n",
"else:\n",
" T[i][j] = T[i - 1][j]\n",
"\n",
"The answer will be in the bottom right corner of the matrix.\n",