首先需要明白 0-1 背包问题中的放置表格,见 “玩转算法面试 从真题到思维全面提升算法思维” 9-5 节,本题思路类似
表格纵向为:只考虑第 [0 …,… index] 种硬币(物品)
表格横向为:需要兑换的金额(背包容量)为 j
表格内容为:在横向和纵向的条件下,最少的硬币(物品)数
即:通过表格列举出,当硬币种类为 [0 …,… index] 种,兑换金额为 j 时,最少的硬币数量


以 Example 1 为例,可以画出下面的表格

Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

硬币&金额 0 1 2 3 4 5 6 7 8 9 10 11
1 0 1 2 3 4 5 6 7 8 9 10 11
2 0 1 1 2 2 3 3 4 4 5 5 6
5 0 1 1 2 2 1 2 2 3 3 2 3

第一行:当只考虑面值为 1 的硬币时,兑换总金额为 0 1 2 3… 时,最少需要 0 1 2 3… 个
从第二行开始是关键,举例来说:
若想求只考虑面值为 1 和 2 的硬币时,兑换总金额为 7 最少有几种情况,即表格里加粗的数字是多少,可以这样考虑:
所求的问题可以拆分成这两种情况:
(1) 不使用面值为 2 的进行兑换,则问题变为 “只考虑面值为 1 的硬币时,兑换总金额为 7 最少有几种情况”,这个问题在第一行已经解决,就是上一格的元素,7
(2) 使用至少一个面值为 2 的进行兑换,首先拿一个 2,这样用了一个硬币,剩余金额为 7 - 2 = 5
子问题变为 “考虑面值为 1 和 2 的硬币,兑换总金额为 5” 最少有几种情况,注意子问题仍还需要 “考虑面值为 1 和 2”,而不是只考虑 1,因为可以使用多次 2!,这个子问题的解就是第二行纵坐标为 5 的格子元素的值,即为 3,加上之前用过的一个 “2”,所以需要 3 + 1 = 4 个。

同样,第三行拆分成 “不使用面值为 5 的进行兑换,即只考虑面值为 1 2” 和 “使用至少一个面值为 5 的进行兑换”
比如第 i 行第 j 列,第 i 行(从0开始)对应的面值是 coins[i],第一种情况为 table[i - 1][j],第二种情况为 table[i][j - coins[i]] + 1,取两者最小值即可。不过要注意如果其中一种情况兑换不了,那格子元素的值只能是能兑换的那种情况的值,如果两种情况都兑换不了,就记为 -1,这样双重循环填满这个表格,右下角的元素即为问题的解。

源自于:http://www.imooc.com/article/288317

用背包问题思路解决 322. Coin Change(完全背包)的更多相关文章

  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. 322. Coin Change选取最少的硬币凑整-背包问题变形

    [抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...

  4. LeetCode 322. Coin Change

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

  5. dp:322. Coin Change 自下而上的dp

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

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

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

  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. [LC] 322. Coin Change

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

随机推荐

  1. deepin系统右键刷新-解决增删改文件没有变化

    deepin 新建/删除/修改-->文件/文件夹后 目录不刷新解决方案 方法1: F5键刷新 方法2: 通过修改配置文件-->调整最大文件监控数量(建议使用这种方式) sudo vim / ...

  2. Gson的序列化和反序列化-待更新

    反序列化为List List<Person> persons =gson.fromJson(json, new TypeToken<List<Person>>() ...

  3. LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化

    问题描述 LG5202 题解 \[opt[i]=xx+(cnt[i]-cnt[yy]<=0)\] 发现\(cnt[i]-cnt[yy] <= 0\)只能有两种取值 于是直接堆优化即可 \( ...

  4. 在Windows系统中安装matplotlib,需要注意的问题

    matplotlib的下载地址:https://pypi.org/project/matplotlib/#files 以python3.6为例,适合的版本matplotlib-3.1.1-cp36-c ...

  5. linux,xshell命令

     一. linux  1.Linux发行版 <1> 常见的发行版本如下: Ubuntu Redhat Fedora openSUSE Linux Mint Debian Manjaro M ...

  6. AntzOs系列导航

    Antz操作系统 一个自制的操作系统,Antz .半图形化半命令式系统,同时嵌入Antzscript脚本语言(写在之后). 自制操作系统Antz(1)——Boot Sector 自制操作系统Antz( ...

  7. 一起学react (1) 10分钟 让你dva从入门到精通

    前言 如果文章中有错误的地方的话 可以直接加我QQ:469373256 自己针对一些问题做的优化版本 目前刚启动 还不是很成熟 https://github.com/fangkyi03/fastkit ...

  8. Springmvc配置定时任务注解开发

    1.添加命名空间和xsd约束 xmlns:task="http://www.springframework.org/schema/task" http://www.springfr ...

  9. element UI 调整表格行高

    使用element UI的table默认属性,绘制表格如下: 该表格的行高太大了,于是想调小一些. 查看官网的文档,table有几个属性, row-style:行的 style 的回调方法,也可以使用 ...

  10. 【编译系统01】编译器 - 词法分析器(lexial)的设计思路

    时间:2019/11/29 首先,词法分析器由一个扫描器与状态机组成. 一. 词法分析器整体设计流程 二.设计细节 1. code.txt: 我们假设读取下面文本 2.符号类型的设计 我们使用 enu ...