原题链接在这里:https://leetcode.com/problems/coin-change/

题目:

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:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

题解:

要求满足amount时用的最少coins数目.

Let dp[i] denotes amount == i 时用的最少coins数目. 用一维array储存.

递推时,状态转移dp[i] = Math.min(dp[i], dp[i-coins[j]]+1).  dp[i-coins[j]]+1表示用最少个数coin表示i-coins[j].

初始化dp[0] = 0. amount为0时不需要硬币. dp其他值都是Integer的最大值, 之后会每次更新取最小值.

Time Complexity: O(amount * coins.length).

Space: O(amount).

AC Java:

class Solution {
public int coinChange(int[] coins, int amount) {
if(amount == 0){
return 0;
} if(coins == null || coins.length == 0){
return -1;
} int [] dp = new int[amount+1];
dp[0] = 0;
for(int i = 1; i<=amount; i++){
dp[i] = Integer.MAX_VALUE;
for(int coin : coins){
if(i-coin<0 || dp[i-coin]==Integer.MAX_VALUE){
continue;
}else{
dp[i] = Math.min(dp[i], dp[i-coin]+1);
}
}
} return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}

类似Minimum Cost For Tickets.

LeetCode Coin Change的更多相关文章

  1. [LeetCode] Coin Change 硬币找零

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

  2. [LeetCode] Coin Change 2 硬币找零之二

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

  3. LeetCode——Coin Change

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

  4. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  5. [LeetCode] 518. Coin Change 2 硬币找零之二

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

  6. [LeetCode] 322. Coin Change 硬币找零

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

  7. [LeetCode] 518. Coin Change 2 硬币找零 2

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

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 【转载】CentOS服务器配置VPN详解

    转载来自: https://bbs.aliyun.com/read/162297.html http://www.wanghailin.cn/centos-7-vpn/ 操作系统:CentOS 6.3 ...

  2. [C++][数据结构]队列(queue)的实现

    对于队列的定义,前人之述备矣. 队列的实现方法与栈非常相似.我直接在我实现的那个栈的代码上加了一点东西,全局替换了一些标识符,就实现了这个队列. 我实现的是一个queue<value>容器 ...

  3. nodejs review-01

    lesson lesson-code 05 Run your first web server 使用curl //指定方法;显示header信息 curl -X GET -i localhost:30 ...

  4. 【TYVJ1864】[Poetize I]守卫者的挑战 概率与期望

    [TYVJ1864][Poetize I]守卫者的挑战 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过."我 ...

  5. Linux 压缩解压

    压缩解压 ------------------------------------------ linux 下所有的压缩格式,WinRAR 都支持 gzip .gz 格式 压缩文件: gzip 文件名 ...

  6. Ubuntu下使用vsftpd实现FTP

    ## 哈哈哈啊哈 被领导啪啪啪打脸,文件连在线打开都不行,你做事情的时候有没有考虑过别人使用时的感受!! 需求: 部门老大希望在内网搭建一个用于员工共享文件的系统. 很自然的就想到通过FTP去实现. ...

  7. table sorting–angularjs

    1: <script type="text/javascript" ng:autobind 2: src="http://code.angularjs.org/0. ...

  8. Main 程序的入口要做哪些事情

    Main 程序的入口要做哪些事: 1.从主类中实例化程序(UIApplication)对象 2.如果有委托的话,从给定的类实例化委托和设置程序(UIApplication) 的代理. 3.开启主事件的 ...

  9. 【BZOJ】3523: [Poi2014]Bricks

    题意 \(n(n \le 1000000)\)个物品,颜色分别为\(a[i]\),现在要求排在一排使得相邻两个砖块的颜色不同,且限定第一个砖块和最后一个砖块的颜色,输出一个合法解否则输出-1. 分析 ...

  10. socket是什么?(翻译)

    根据stackoverflow的答案: 原文:A socket represents a single connection between two network applications. The ...