题解 | #兑换零钱(一)#

兑换零钱(一)

https://www.nowcoder.com/practice/3911a20b3f8743058214ceaa099eeb45

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 最少货币数
# @param arr int整型一维数组 the array
# @param aim int整型 the target
# @return int整型
#
import math

class Solution:
    def minMoney(self , arr: List[int], aim: int) -> int:
        # write code here
        f = [0] + [math.inf] * aim
        for x in arr:
            for c in range(x, aim + 1):
                f[c] = min(f[c], f[c - x] + 1)
        return f[-1] if f[-1] < math.inf else -1

全部评论

相关推荐

AC鸽想进大厂:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务