[抄题]:

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:

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

Example 2:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

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

[思维问题]:

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

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

[一句话思路]:

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

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

记得初始化

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

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

[关键模板化代码]:

[其他解法]:

  1. class Solution {
  2. public int coinChange(int[] coins, int amount) {
  3. //corner case
  4. if (coins == null || coins.length <= 0 || amount <= 0) return -1;
  5.  
  6. //initialization: dp[] and fill, and the first num
  7. int[] dp = new int[amount + 1];
  8. Arrays.fill(dp, amount + 1);
  9. dp[0] = 0;
  10.  
  11. //for loop for each coins, each amount
  12. for (int j = 0; j < coins.length; j++) {
  13. for (int i = 0; i <= amount; i++) {
  14. if (i - coins[j] >= 0)
  15. dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
  16. }
  17. }
  18.  
  19. //return if dp[] is normal value, or just -1
  20. return dp[amount] > amount ? -1 : dp[amount];
  21. }
  22. }

[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. 细谈getRequestDispatcher()与sendRedirect()的区别

    问题?细谈getRequestDispatcher()与sendRedirect()的区别 首先我们要知道: (1)request.getRequestDispatcher()是请求转发,前后页面共享 ...

  2. 支持Oracle的模糊查询和精准查询

    相信所有的软件开发者都做过页面上的查询功能,而且很多都需要既支持模糊查询的,也需要支持精准查询的,而且不需要增加多余的功能,只需要在文本框中输入包含类似*之类的符号即可. 下面的方法就是通过*来判断到 ...

  3. IntelliJ IDEA 2018.3 升级功能介绍

    |0前言 2018.11.28 IntelliJ IDEA 2018.3 正式版发布.对于一个忠实爱好者,迫不及待的我下载了最新版本来体验下.而且 IDEA 今年的第三次重大更新提供了不容错过的显著功 ...

  4. hasura graphql-engine 集成zombodb

    zombodb 是一个很不错的pg 扩展,可以方便的把es 与pg 集成起来,使用方便 ,目前尽管有一些docker 镜像 但是版本都比较老,所以基于centos7 做了一个新的docker 镜像,同 ...

  5. LOJ 2546 「JSOI2018」潜入行动——树形DP

    题目:https://loj.ac/problem/2546 dp[ i ][ j ][ 0/1 ][ 0/1 ] 表示 i 子树,用 j 个点,是否用 i , i 是否被覆盖. 注意 s1<= ...

  6. Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端

    WebService服务端项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...

  7. HTTP客户端/服务端 POST/GET

    获取GET请求内容 实例 //引入模块var http=require('http');var urls=require('url');var util=require('util');//创建服务h ...

  8. JavaScript开发中使用频率较高的一些方法

    1.填充字符串 ES7推出了字符串补全长度的功能.如果某个字符串不够指定长度,会在头部或尾部补全. String.prototype.padStart(maxLength, fillString=’ ...

  9. Windows程序设计_21_Win32文件操作

    没什么新的内容,自己的练习代码,供大家点评. /* Windows系统编程--实例 1)复制文件 */ #define UNICODE //#define _UNICODE #include < ...

  10. MOBA英雄AI设计分享

    转自:http://www.gamelook.com.cn/2018/07/333877 文/wataloo 1  设计概要 1.1  设计原则和目的 英雄AI的目的主要有: 1.新手过渡局,让玩家刚 ...