Skip to content

Instantly share code, notes, and snippets.

@caretcaret
Created March 10, 2015 21:59
Show Gist options
  • Save caretcaret/a2088336aa63b6cc94cf to your computer and use it in GitHub Desktop.
Save caretcaret/a2088336aa63b6cc94cf to your computer and use it in GitHub Desktop.
from __future__ import print_function
def inverse_coinstar(coins):
N = len(coins)
value = sum(coins)
memo = {}
# ways to make n from coins[:i]
def ways_to_make(n, i):
if n == 0:
return 1
if n < 0 or i == 0:
return 0
result = ways_to_make(n, i-1) + ways_to_make(n - coins[i-1], i-1)
memo[n, i] = result
return result
if value % 2 == 0:
return ways_to_make(value / 2, N)
return 0
test_cases = [[1, 1], [1, 1, 1, 1], [2, 3]]
for test in test_cases:
print(test, inverse_coinstar(test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment