The minimum number of coins the sum of which is S

后端 未结 12 1379
我寻月下人不归
我寻月下人不归 2020-11-30 05:06

Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type a

12条回答
  •  孤街浪徒
    2020-11-30 05:48

    def leastCoins(lst, x):
    temp = []
    if x == 0:
        return 0
    else:       
        while x != 0:
            if len(lst) == 0:
                return "Not Possible"
            if x % max(lst) == 0:
                temp.append((max(lst), x//max(lst)))
                x = 0
            elif max(lst) < x:
                temp.append((max(lst), x//max(lst)))
                x = x % max(lst)
                lst.remove(max(lst))
            else:
                lst.remove(max(lst))
    return dict(temp) 
    

    leastCoins([17,18,2], 100652895656565)

提交回复
热议问题