- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下是问题的链接:https://leetcode.com/problems/coin-change/
问题:
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
示例1:
Input:
coins = [1, 2, 5]
,amount = 11
Output:
3
Explanation:
11 = 5 + 5 + 1
示例2:
Input:
coins = [2]
,amount = 3
Output:
-1
我想出了以下代码,它通过了上面示例 1 中的测试用例,但没有通过示例 2 中的测试用例
这是我的代码,遵循解决方案描述的算法(附截图)
以下代码返回 0,而我期望返回 -1,因为结果等于 sys.maxsize
import sys
#declare global look up dictionary to store key (amount) and value (minimum number of coins)
lookup_dict = {}
#declare minimum number of coins and use the maximum number in python as place holder
minimum = sys.maxsize
def coinChange(coins,amount):
result = coinChangeAux(coins, amount)
return -1 if result==sys.maxsize else result
def coinChangeAux(coins, amount):
#since we will modify minimum and lookup_dict, we put "global" in front of them
global minimum,lookup_dict
if amount in lookup_dict.keys():
return lookup_dict[amount]
if amount <= 0:
lookup_dict[amount]=0
return lookup_dict[amount]
if not coins:
return -1
for i in coins:
if amount>=i:
minimum = min(minimum,coinChange(coins,amount-i)+1)
lookup_dict[amount] = minimum
return lookup_dict[amount]
coins=[2]
amount = 3
coinChange(coins,amount)
我尝试通过仅在下面的 coinChange 函数中返回结果来调试上面的代码,结果确实返回 9223372036854775807 ,它等于 sys.maxsize (所以我预计上面的 coinChange 函数应该返回 -1,因为结果等于 maxsize
import sys
#declare global look up dictionary to store key (amount) and value (minimum number of coins)
lookup_dict = {}
#declare minimum number of coins and use the maximum number in python as place holder
minimum = sys.maxsize
def coinChange(coins,amount):
result = coinChangeAux(coins, amount)
return result
def coinChangeAux(coins, amount):
#since we will modify minimum and lookup_dict, we put "global" in front of them
global minimum,lookup_dict
if amount in lookup_dict.keys():
return lookup_dict[amount]
if amount <= 0:
lookup_dict[amount]=0
return lookup_dict[amount]
if not coins:
return -1
for i in coins:
if amount>=i:
minimum = min(minimum,coinChange(coins,amount-i)+1)
lookup_dict[amount] = minimum
return lookup_dict[amount]
coins=[2]
amount = 3
coinChange(coins,amount)
最佳答案
当你递归时,你调用coinChange
,这会将sys.maxsize
变成-1。因此,minimum
的新值将为 −1 + 1 = 0。(sys.maxsize + 1
在 Python 中不会溢出,这是一件好事。)
仅调用包装函数coinChange
一次,并递归调用coinChangeAux
:
for i in coins:
if amount >= i:
minimum = min(minimum, coinChangeAux(coins, amount - i) + 1)
关于python - 无法弄清楚为什么我的代码不适用于特定情况(Leetcode 的硬币找零),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925669/
我目前正在做一个项目,试图开发一种用于 P2P 能源交易的货币和模型,其中每产生一千瓦时的可再生能源就会向该生产商类型转换一枚硬币。我的问题是关于销毁智能合约中的代币。 所有与我的项目类似的当前实现都
在没有Maps帮助的情况下通过Memoization解决问题,由于读取文件的方法,我得到了TLE,根据我的说法,这不应该是这种情况。可能的原因是什么? 这是给出 AC - http://ideone.
考虑下面这段伪代码,其中d是面额值数组,k是面额数,n是要进行更改的金额。 Change(d; k; n) 1 C[0] 我真的不明白这部分,你为什么要用它,谁能给我解释一下! 最佳答案 为了回答
我正在尝试在我的网站上实现 Coin Slider (http://workshop.rs/2010/04/coin-slider-image-slider-with-unique-effects/)
我有使用硬币 slider 的画廊 var $jq = jQuery.noConflict(); $jq(window).load(function() { var imhei
我使用了从该站点提取的硬币 slider http://workshop.rs/projects/coin-slider/ .它现在自动滚动并仅在悬停时显示上一个和下一个。我需要禁用自动滚动并正常显示
我的问题是一道CodeFu练习题(2012 round 2 problem 3)。它基本上归结为将整数数组分成两个(几乎)相等的两半并返回两者之间可能的最小差异。我在下面包含了问题描述。如评论中所述,
我们的老师要求我们制作一 jar 硬币,用来计算我们有多少便士、一毛钱等,然后给出总金额。 这是他希望我们使用的模板 https://online.pcc.edu/content/enforced/7
我正在尝试使用币安币 future 的 api 下载 BTC/USD 永续 future 的历史价格数据,具体来说,我想使用 this endpoint .但是,我找不到必须为 BTC/USD 指定的
我上周刚开始学习计算机科学,我们得到了一个名为“硬币”的工作表,其中我必须找出一组硬币中有多少个 25 美分、10 美分、5 美分和 10 便士。我遇到了很多麻烦,并收到了该错误。这是我的代码 pac
我正在构建一些使用消耗性硬币的测验。我使用 NSUserDefault 来保存设备上的硬币及其工作。我还在 qiuz 中使用 CloudKit 处理数据。 不麻烦的是,如果用户切换设备如何恢复硬币?有
我是一名优秀的程序员,十分优秀!