Fixed pull request changes

This commit is contained in:
mrb00l34n
2015-07-22 11:31:38 -04:00
parent 3370106d36
commit e8de164d9d
4 changed files with 27 additions and 27 deletions

View File

@@ -33,7 +33,7 @@
"## Hints\n",
"\n",
"* Can you think of a way to build up to a solution?\n",
"* If there are 2 ways of making 3, and you are now given a coin of value v, how many ways can you make 3+v?\n",
"* If there are 2 ways of making 3, and you are now given a coin of value v, how many ways can you make 3 + v?\n",
"* Can you think of a way to divide the problem into smaller subproblems?"
]
},
@@ -46,15 +46,15 @@
"One possible solution using dynamic programming:\n",
"* Create an array, s.t arr[i] = # of ways to make change for i\n",
"* Initialize arr[0] = 1, arr[i>0] = 0\n",
"* For each coin, and for each index from coin to n, increment arr[i] by arr[i-coin]\n",
"* For each coin, and for each index from coin to n, increment arr[i] by arr[i - coin]\n",
"\n",
"How does this work?\n",
"* As we iterate through each coin, we are adding the ways of making arr[i-coin] to arr[i]\n",
"* As we iterate through each coin, we are adding the ways of making arr[i - coin] to arr[i]\n",
"* If we have 2 ways of making 4, and are now iterating on a coin of value 3, there should be 2 ways of making 7.\n",
"* We are essentially adding the coin we are iterating on to the # of ways of making arr[i].\n",
"\n",
"Complexity:\n",
"* Time: O(mn)\n",
"* Time: O(mn); let the number of coins be m. We iterate from arr[coin] -> arr[n], or ~ n operations on each coin, hence n*m. \n",
"* Space: O(n)"
]
},
@@ -73,12 +73,12 @@
},
"outputs": [],
"source": [
"def change_ways(n,k,coins):\n",
" arr = [1] + [0] * n\n",
" for coin in coins:\n",
" for i in xrange(coin,n + 1):\n",
" arr[i] += arr[i - coin]\n",
" return 0 if n == 0 else arr[n]"
"def change_ways(n, coins):\n",
" arr = [1] + [0] * n\n",
" for coin in coins:\n",
" for i in range(coin, n + 1):\n",
" arr[i] += arr[i - coin]\n",
" return 0 if n == 0 else arr[n]"
]
},
{
@@ -112,9 +112,9 @@
"class Challenge(object):\n",
"\n",
" def test_coinchange_ways(self,solution):\n",
" assert_equal(solution(0,2,[1,2]), 0)\n",
" assert_equal(solution(100,3,[1,2,3]), 884)\n",
" assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645)\n",
" assert_equal(solution(0, [1, 2]), 0)\n",
" assert_equal(solution(100, [1, 2, 3]), 884)\n",
" assert_equal(solution(1000, range(1, 101)), 15658181104580771094597751280645)\n",
" print('Success: test_coinchange_ways')\n",
"\n",
"\n",