-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcoinchange.py
36 lines (34 loc) · 1.27 KB
/
coinchange.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def solve_dp(coins, change):
"""Coin change problem solver using dynamic programming
Refer to wikipedia for the description: http://en.wikipedia.org/wiki/Coin_problem
And to this blog for a nice explanation: http://interactivepython.org/runestone/static/pythonds/Recursion/recursioncomplex.html
And here fo a half working implementation: http://bryceboe.com/2009/11/04/dynamic-programming-%E2%80%93-coin-change-problem-in-python/
"""
table = [None for x in xrange(change + 1)]
table[0] = []
for i in xrange(1, change + 1):
for coin in coins:
length = 0
if table[i - coin] != None:
length = len(table[i - coin])
if coin > i: continue
elif not table[i] or (length + 1 < len(table[i])):
if table[i - coin] != None:
table[i] = table[i - coin][:]
table[i].append(coin)
return len(table[-1]) if table[-1] != None else 0, table[-1] if table[-1] != None else None
def solve_gready(coins, change):
biggest_first = reversed(sorted(coins))
accumulator = change
solution = []
for number in biggest_first:
if number <= accumulator:
solution.append(number)
accumulator -= number
if accumulator == 0:
break
if accumulator != 0:
solution = None
return len(solution) if solution != None else 0, solution