硬币最小数量

Description

Given the list of coins of distinct denominations and total amount of money. Output the minimum number of coins required to make up that amount. Output -1 if that money cannot be made up using given coins. You may assume that there are infinite numbers of coins of each type.

Input

The first line contains 'T' denoting the number of test cases. Then follows description of test cases. Each cases begins with the two space separated integers 'n' and 'amount' denoting the total number of distinct coins and total amount of money respectively. The second line contains N space-separated integers A1, A2, ..., AN denoting the values of coins.

Output

Print the minimum number of coins required to make up that amount or return -1 if it is impossible to make that amount using given coins.

Sample Input
2
3 11
1 2 5
2 7
2 6
Sample Output
3
-1

思路

动态规划,变成小问题递归求解

如果有硬币[1, 2, 5], 总量11

从上到下的动态规划:

选取0个1, 递归[2, 5], 11;选取一个1,递归[2, 5], 10; 或者选2个1,递归[2, 5], 9.......

递归结束条件是当前只有一个硬币的时候,判断总量是否能整除硬币面值,如果能返回数量,不然返回-1或者其他“不正常”的值。

需要注意的是,从上到下的方式很多时候可能是重复调用,比如上面的例子,选取一个1,一个2,递归[5], 8; 和选取三个1,0个2,递归[5], 8是一样的。为了避免重复调用,可以使用一个map记录调用的参数信息,如果调用过,直接返回结果。这里递归的硬币就可以使用当前硬币在数组中的位置参数index。

python
import functools
def solve(coins, amount):
    @functools.lru_cache(None)
    def dp(i, amount):
        if amount == 0:
            return 0
        if i == 0:
            return [float('inf'), amount // coins[0]][amount % coins[0] == 0]

        ans = float('inf')
        for j in range(amount//coins[i] + 1):
            ans = min(ans, j + dp(i-1, amount-coins[i]*j))
        return ans

    res = dp(len(coins)-1, amount)
    return [res, -1][res == float('inf')]


for _ in range(int(input())):
    _, amount = map(int, input().split())
    coins = list(map(int, input().split()))
    print(solve(coins, amount))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 一江月,二江月,月盈天净风语迟。钟声惊梦人。
    韶星祜阅读 193评论 0 5
  • 明天就要去文博城看开幕式了,今天晚上我的心情非常的激动,我不停的想着明天去文博城看开幕式的样子!在开幕式上...
    实验小学五三班王元昊阅读 188评论 0 0
  • 从古至今,环境对人们的影响,可谓至关重要。然而,整理的神奇,在一个人的生命行进轨迹中,竟发挥着如此微妙,却又如此具...
    悄然Edward阅读 401评论 0 7