标签:rate ext cut hang lis review interview [1] ret
Write a function that given a target amount of money and a list of possible coin denominations, returns the number of ways to make change for the target amount using the coin denominations
Write out your work on paper/pencil, then see if you can code up your solution
This is a classic interview problem, so classic that you‘ve already seen a very similar problem in the recursion section! Make sure to review that problem first before reading our solution here!
In this solution we will use a bottom-up algorithm.
def solution(n, coins): # Set up our array for trakcing results arr = [1] + [0] * n for coin in coins: for i in range(coin, n + 1): arr[i] += arr[i - coin] if n == 0: return 0 else: return arr[n]
solution(100, [1, 2, 3])
This solution results in O((m)(n)) with m being the number of coins, where we iterate about n operations. This is O(n) space.
标签:rate ext cut hang lis review interview [1] ret
原文地址:http://www.cnblogs.com/prmlab/p/6961049.html