问题是给出一个产品价格列表,如 [2,3,5,1,1,2,1] 和预算为 5,输出应该是可以购买的最大产品数量。对于这个是 4 ([1,1,2,1])
我的代码如下,有时它可以工作,但对于像[2,3,5,1]这样的价格,预算= 7,它应该是3,但它是2。你们能帮忙检查我代码的哪一部分是错误的吗?谢谢
def getMaximumOutfits(money,outfits):
result = []
spent = 0
max_length = 0
for i in range(len(outfits)):
spent+=outfits[i]
if spent <=money:
if i!=len(outfits)-1:
result.append(outfits[i])
else:
result.append(outfits[i])
if max_length < len(result):
max_length = len(result)
else:
if max_length<len(result):
max_length = len(result)
result=[]
spent = outfits[i]
if spent <= money:
result.append(outfits[i])
print(max_length)
Cats萌萌
慕雪6442864
相关分类