原题链接在这里: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. 触发器--mysql

    SHOW TRIGGERS;查看所有触发器 create trigger tg1 after insert on user for each row beginupdate user set name ...

  2. poj 2955 Brackets

    题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...

  3. _stdcall,_cdecl区别

    (1) _stdcall调用 _stdcall是Pascal程序的缺省调用方式,参数采用从右到左的压栈方式,被调函数自身在返回前清空堆栈. WIN32 Api都采用_stdcall调用方式,这样的宏定 ...

  4. Ubuntu防火墙设置

    转载自:http://baisongfly.blog.163.com/blog/static/30135117200923005956159/ 1.安装 sudo apt-get install uf ...

  5. google 在线代理浏览

    谷歌访问不了,你又N多方法,比如搭建VPN,买VPN,或查找google多个IP访问, 或通过第三方反代理网站访问, 或通过客户端代理(类似goagent)等 下面罗列出可以访问google的几个代理 ...

  6. ORA-20011 ORA-29913 and ORA-29400 with Associated KUP-XXXXX Errors from DBMS_STATS.GATHER_STATS_JOB(Doc ID 1274653.1)

    首先在alert log裡面頻繁的看見如下錯誤: DBMS_STATS: GATHER_STATS_JOB encountered errors.  Check the trace file. Err ...

  7. js特效2

    侧边栏简单版 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...

  8. MongoDB学习笔记(1):MongoDB简介

    1. MongoDB的特点: (1) 易于使用 MongoDB是一个面向文档的数据库,非关系型数据库.通过在文档中嵌入式文档和数据,面向对象的方法能够仅使用一条记录来表现复杂的层次关系.文档的键和值不 ...

  9. Linux琐碎

    本周接触Linux的内容: 1.netstat -tanlp 显示监听的所有端口并且不解析端口为属于哪个进程 history | grep cmd 从命令历史中找到需要的命令 2. scp命令的使用: ...

  10. to_string()的应用

    作用是将数字转化为字符串 #include<iostream> #include<cmath> #include<algorithm> #include<cs ...