1 Star 0 Fork 0

蓝桥云课/python-100

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
061-change_min.py 777 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiaoyi733112 提交于 2020-03-03 17:57 . python-100 answer
import sys
class CoinChanger(object):
def make_change(self, coins, total):
if coins is None or total is None:
raise TypeError('coins or total cannot be None')
if not coins or total == 0:
return 0
cache = {}
return self._make_change(coins, total, cache)
def _make_change(self, coins, total, cache):
if total == 0:
return 0
if total in cache:
return cache[total]
min_ways = sys.maxsize
for coin in coins:
if total - coin < 0:
continue
ways = self._make_change(coins, total - coin, cache)
if ways < min_ways:
min_ways = ways
cache[total] = min_ways + 1
return cache[total]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lanqiao-courses/python-100.git
git@gitee.com:lanqiao-courses/python-100.git
lanqiao-courses
python-100
python-100
master

搜索帮助