[抄题]:

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

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

Example 2:

Input: coins = [2], amount = 3
Output: -1

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

dp数组运行了之后才有正常值,所以需要用arrays.fill初始化为奇葩值

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Math.min(dp[目标值], dp[目标值 - 当前值] + 1);

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

记得初始化

[复杂度]:Time complexity: O(n2) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

class Solution {
public int coinChange(int[] coins, int amount) {
//corner case
if (coins == null || coins.length <= 0 || amount <= 0) return -1; //initialization: dp[] and fill, and the first num
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0; //for loop for each coins, each amount
for (int j = 0; j < coins.length; j++) {
for (int i = 0; i <= amount; i++) {
if (i - coins[j] >= 0)
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
} //return if dp[] is normal value, or just -1
return dp[amount] > amount ? -1 : dp[amount];
}
}

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

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 零钱兑换

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

  4. 【Leetcode】322. Coin Change

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

  5. 用背包问题思路解决 322. Coin Change(完全背包)

    首先需要明白 0-1 背包问题中的放置表格,见 “玩转算法面试 从真题到思维全面提升算法思维” 9-5 节,本题思路类似表格纵向为:只考虑第 [0 …,… index] 种硬币(物品)表格横向为:需要 ...

  6. LeetCode 322. Coin Change

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

  7. 322. Coin Change零钱兑换

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

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

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

  9. 322. Coin Change

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

随机推荐

  1. Linux系统安装管理

    将lfs linux liveCD的内容copy安装到硬盘 先将98.ima(dos启动软盘镜像文件)用ultraISO写入到u盘(usbhdd+), 不必勾选“创建启动分区”. 将liveCD和内核 ...

  2. dmi-ipmi

    api,cli,gui,tui,dmi(smbios),ipmi,bios,efi,uefi SMBIOS(System Management BIOS)是主板或系统制造者以标准格式显示产品管理信息所 ...

  3. 解决 windows下安装Anaconda后python pip不可用的情况

    在windows系统下通过安装Anaconda的方式安装的python使用中发现不能再通过pip安装python包.只能通过conda install packname 的方法,导致很多conda不支 ...

  4. 十五、springcloud(一)注册中心Eureka

    1.Eureka的基本架构 a.Eureka Server 提供服务注册和发现 b.Service Provider 服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到 c.Servi ...

  5. 根据CPU核心数确定线程池并发线程数

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  6. VUE中如何优雅的动态绑定长按事件

    答案没有: 图片是从后端传过来, 加到imgTarget属性,实现长按点击删除该图片 let img = document.createElement('img'); img.src = " ...

  7. Windows Azure Virtual Network (13) 跨数据中心之间的虚拟网络点对点连接VNet Peering

    <Windows Azure Platform 系列文章目录> 今天是大年初二,首先祝大家新年快乐,万事如意. 在笔者之前的文章中:Windows Azure Virtual Networ ...

  8. Microsoft Azure News(7) Azure B系列虚拟机

    <Windows Azure Platform 系列文章目录> 最近微软Azure新数据中心上线了B系列的虚拟机,我这边研究了一下,给大家分享. Azure B系列虚拟机,其实是Burst ...

  9. keil5 MDK warning:registered ARM compiler version not found in path

    重装 打开keil5弹出窗口: warning:registered ARM compiler version not found in path... 解决: 增加系统环境变量 ARMCC5LIB ...

  10. sync;sync;sync;reboot

    Sync命令 在用reboot命令启动unix系统后,系统提示出错信息,部分应用程序不能正常工作.经仔细检查系统文件,并和初始的正确备份进行比较,发现某些文件确实被破坏了,翻来覆去找不到文件遭破坏的原 ...