mycode

我开始错误的思路:先用大钱除总钱数来保证 fewest number of coins,当最后剩下的amount小于最小币值的货币时,就说明return -1,但是这样想是有问题的!!!

例如:[1,4,5]   12=5*2+2    12 =5*2 + 1*2 用了4枚,但是12 = 4*3

因为还有下一步,也就是用次大的币值去重复这个步骤!!而且也不能直接去使用次大的,而是先把最大币值的数目-1,-2,-3.。。。依次看

参考:

思路:类似于上一个62.unique paths,就要要考虑能到达该点的路径中的上一个点的情况

class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
n = len(coins)
# dp[i]表示amount=i需要的最少coin数
dp = [float("inf")] * (amount+1)
dp[0] = 0
for i in range(amount+1):
for j in range(n):
# 只有当硬币面额不大于要求面额数时,才能取该硬币
if coins[j] <= i:
dp[i] = min(dp[i], dp[i-coins[j]]+1)
# 硬币数不会超过要求总面额数,如果超过,说明没有方案可凑到目标值
return dp[amount] if dp[amount] <= amount else -1

leetcode-mid-dynamic programming-322. Coin Change - NO的更多相关文章

  1. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  2. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  4. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  6. 【Leetcode】322. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

  8. 322. Coin Change零钱兑换

    网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...

  9. 322 Coin Change 零钱兑换

    给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...

  10. [LC] 322. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

随机推荐

  1. springboot中的编码设置

    在springboot中编码配置可以通过filter也可以通过springboot的核心配置文件application.properties中配置如下信息: #配置字符编码spring.http.en ...

  2. springcloud-概念

    springcloud-概念 一.架构演进过程 单体架构----分布式架构----SOA(eg.dubbo)服务治理架构----微服务 随着互联网的发展,需求的激增致使网站应用规模的扩大,最后转成了技 ...

  3. 剑指offer-1:旋转数组的最小数字

    旋转数组的最小数字1.题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2 ...

  4. 解析html,提取元素参数

    r = s.get(loginurl, verify=False) dom = etree.HTML(r.content.decode("utf-8")) try: result[ ...

  5. Docker下载镜像出现failed to register layer: symlink....问题

    在用Docker下载RabbitMQ的时候出现如下问题 个人解决方案:重启Docker. 若重启还是无法解决问题,可以先关闭Docker systemctl stop docker 然后把已下载的相关 ...

  6. 关于 i++ 和 ++ i

    先看一下代码,猜想一下输出值 @Testpublic void test() { int i =1; int a,b=0; i++; a=(i++); System.out.println(a); S ...

  7. PHP中替换换行符的几种方法

    PHP中替换换行的几种方法 参考脚本之家的文章:<PHP中替换换行符的几种方法小结>. 代码: 方法一: $replace_str = str_replace(array("\r ...

  8. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  9. Codeforces Round #593 (Div. 2) C. Labs A. Stones

    题目:https://codeforces.com/contest/1236/problem/A 思路:两种操作收益都是3 且都会消耗b 操作2对b消耗较小 则可优先选择操作2 再进行操作1 即可得到 ...

  10. java课堂作业4

    第一题 字符串加密问题 1.程序设计思想 读入字符串,然后获取其长度,利用charAt()获取每个位置字符并且对字符加3实现加密处理,并存入新字符串中.如果遇到xyz则减26存入. 2.程序流程图 3 ...