leetcode-mid-dynamic programming-322. Coin Change - NO
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的更多相关文章
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- 【Leetcode】322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
- 322. Coin Change零钱兑换
网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...
- 322 Coin Change 零钱兑换
给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...
- [LC] 322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
随机推荐
- jQuery全选功能
$(document).ready(function(){ //为父按钮添加事件 $("#chk_all").click(function(){ var a=$("#ch ...
- 9.css3动画-2D/3D变形--trasform
transform: None不转换. Translate(x,y)通过设置X轴的值进行移动. translateY(y)通过设置Y轴的值进行移动. Scale(x,y)定义2D缩放. ScaleX( ...
- vue history模式 ios微信分享坑
vue history模式 ios微信分享坑 问题分析:因为苹果分享会是调取签名失败是因为:苹果在微信中浏览器机制和安卓不同,有IOS缓存问题,和IOS对单页面的优化问题,通俗点说安卓进行页面跳转分享 ...
- Scala Nothing 从官方DOC翻译
Nothing is - together with scala.Null - at the bottom of Scala's type hierarchy. Scala中的Nothing和Null ...
- Laravel - 验证码(captcha)
首先,登录网址 packagist.org 查找 laravel captcha,找到mews/captcha ,根据 packagist 上的使用方法一步步来实现验证码的安装.配置composer. ...
- 001-cut 的用法
[root@zabbix ~]# , /etc/passwd root: bin: daemon: adm: shutdown: halt: mail: operator: games: nobody ...
- DeepFaceLab更新至2019.12.19
简而言之就是:人脸转换更加稳定和精确,切脸上下边界对齐,增加侧脸和嘴巴的识别面积,所以在这个版本之前的模型需要额外的训练.好消息是,如果你目前素材的嘴巴和侧脸识别有问题,可以重新提取脸部. 之前和之后 ...
- ubuntu 16.04 安装Opencv-3.2.0_GPU 与 opencv_contrib-3.2.0
1.准备依赖库 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config ...
- 更新Navicat Premium 后打开数据库出现1146 - Table 'performance_schema.session_variables' doesn't exist
更新Navicat Premium 后打开数据库出现1146 - Table 'performance_schema.session_variables' doesn't exist 解决方法:打开终 ...
- electron启动出现短暂的白屏
mainWindow = new BrowserWindow({ height: 600, width: 960, frame: false, minWidth: 710, minHeight: 50 ...