原题链接在这里: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. LATTICE USB下载线接口说明

    这节知识摘抄于网络,仅用几张图片来显示,提醒Usb下载线的连接方式. 最后贴出个人使用的8线的USB下载器的连接方式: 再补充一点,应为彩色杜邦线与USB下载器是活口连接,两个可以分开,再连接的时候, ...

  2. python:列表与元组

    1.python包含六种内建的序列,列表和元组是其中的两种,列表可以修改,元组则不能 2.通用序列操作 2.1 索引:和C#的区别是索引可以为负数,最后一个元素索引为-1,索引超出范围会报错 例:&g ...

  3. 【转载】PyQt QSetting保存设置

    转载地址: http://blog.sina.com.cn/s/blog_4b5039210100h3zb.html 用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小,位 ...

  4. 【Java EE 学习 32 下】【JQuery】【JQuey中的DOM操作】

    一.JQuery中的DOM操作. 什么是DOM:DOM是一中和浏览器.平台.语言无关的接口,使用该接口可以轻松访问页面中所有的标准组件.DOM简称文档对象模型,是Document Oject Mode ...

  5. Inno Setup 版本 5.5.3+ 简体中文语言包

    ; *** Inno Setup 版本 + 简体中文消息 *** [LangOptions] LanguageName=<7B80><4F53><> Languag ...

  6. IBM x3850 x5 服务器 安装 Windows Server 2008

    一.硬件需求 一个8G以上的U盘 二.软件需求 1.Windwos Server 2008镜像 2.系统启动盘制作工具Ultraiso 3.IBM ServerGuide引导镜像 三.制作及安装步骤 ...

  7. tcp三次握手和四次握手

    建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...

  8. android解析json

    android2.3提供的json解析类 android的json解析部分都在包org.json下,主要有以下几个类: JSONObject:可以看作是一个json对象 JSONStringer:js ...

  9. ZeroMQ接口函数之 :zmq_z85_decode – 从一个用Z85算法生成的文本中解析出二进制密码

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_z85_decode zmq_z85_decode(3)         ØMQ Manual - ØMQ/4.1 ...

  10. *HDU3367 最小生成树

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...