[抄题]:

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. 程序猿大牛 Jeff Atwood 的两本中文书

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/happydeer/article/details/17094271 watermark/2/text ...

  2. php最常见最经典的算法题

    1.一群猴子排成一圈,按1,2,…,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫 ...

  3. Spring Boot - 项目构建与解析

    构建 Maven 项目 通过官方的 Spring Initializr 工具来产生基础项目,访问 http://start.spring.io/ ,如下图所示,该页面提供了以Maven构建Spring ...

  4. [蓝桥杯]PREV-10.历届试题_幸运数

    问题描述 幸运数是波兰数学家乌拉姆命名的.它采用与生成素数类似的“筛法”生成 . 首先从1开始写出自然数1,,,,,,.... 就是第一个幸运数. 我们从2这个数开始.把所有序号能被2整除的项删除,变 ...

  5. PAT 甲级 1005 Spell It Right (20 分)

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  6. [转][C#]文件流读取

    { internal static class FileUtils { public static string GetRelativePath(string absPath, string base ...

  7. studio--常见设置

    13.Butterknife插件:zelezny 12.android studio怎么设置打开项目时打开项目列表? 11.stuido   代码背景颜色设置为护眼模式 ======== 13.But ...

  8. NFS服务配置

    FS服务会经常用于在网络上共享存储. 比如有3台机子A,B,C;他们都需要访问同一个目录,使用NFS, 只需要把图片都放在A上,然后A共享给B和C即可. 访问B和C时,是通过网络的方式访问A上的哪个目 ...

  9. Let'sencrypt.sh 抛出异常: Response: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)>

    起因 今天网站的SSL证书过期了,打算重新申请,运行 Let'sencrypt.sh 的时候抛出了这么个异常. 一番搜索,发现居然找不到直接的答案.没有直接的答案就只能通过间接的答案来解决了. 希望我 ...

  10. linux git clone 指定分支

    git clone -b develop http://192.168.11.11:8888/scm/git/vrmmo 指定下载develop分支