Level:

  Medium

题目描述:

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

Note:

You may assume that you have an infinite number of each kind of coin.

思路分析:

  动态规划的思想,我们用dp[ i ] 代表,当amount的为 i 时的最小找零数,dp[ 0 ]=0。那么 dp[ i ]我们初始化为 amount+1,因为dp[ i ]的最大值不超过amount(只有零钱1元的情况),则状态转移方程为if(coins[t]<=i) dp[ i ]=min(dp[ i ],dp[ i-coins[ t ]]+1)。最后判断dp[amount],如果大于amount,那么返回-1,否则返回dp[amount]。

代码:

public class Solution{
public int coinChange(int []coins,int amount){
if(coins==null||coins.length==0||amount<0)
return -1;
int []dp=new int[amount+1];
dp[0]=0;
for(int i=1;i<dp.length;i++){
dp[i]=amount+1;//初始化dp[i]
}
for(int j=1;j<=amount;j++){
for(int t=0;t<coins.length;t++){
if(coins[t]<=j){
dp[j]=Math.min(dp[j],dp[j-coins[t]]+1);//状态转移可以这样理解,如果amount==11,如果现在取到一个五块,那么只要知道dp[6],则dp[11]就是dp[6]+1;
}
}
}
return (dp[amount]>amount)?-1:dp[amount];
}
}

53.Coin Change(找硬币)的更多相关文章

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

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

  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] 518. Coin Change 2 硬币找零之二

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

  4. UVA 674 Coin Change 换硬币 经典dp入门题

    题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数. 经典的dp题...可以递推也可以记忆化搜索... 我个人比较喜欢记忆化搜索,递推不是很熟练. 记忆化搜索:很白 ...

  5. [LeetCode] Coin Change 硬币找零

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

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

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

  7. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  8. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  9. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

随机推荐

  1. vue 项目,获取手机验证码和图形验证码(iviewUI框架)

    1.编辑获取验证码模块 <Form ref="phoneFormItem" :model="phoneFormItem" :label-width=&qu ...

  2. 关于微信小程序的一些总结

    mpvue? {{}} 在vue和小程序中的区别? 01 小程序中{{}}和vue中的{{}}用法基本一致,可以显示data中的数据,可以写表达式 不一样的地方? 01 小程序的{{}}可以写在属性中 ...

  3. Linux:使用awk命令获取文本的某一行,某一列

    无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家.教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家.点 这里 可以跳转到教程.”. 1.打印文件的第一列( ...

  4. 自定义的最简单的可回调的线程任务CallbackableFeatureTask(模仿google的ListenableFutureTask)

    1.使该Task继承Callable,Runable import java.util.concurrent.Callable; import java.util.function.Consumer; ...

  5. smbrun - smbd和外部程序间的接口程序。

    总览 SYNOPSIS smbrun shell-command 描述 DESCRIPTION 此程序是samba套件的一部分. smbrun是个非常小的“粘合”程序,用于为smbd守护程序smbd( ...

  6. Matomo(Piwik)安装说明-----------基于LNPM环境

    Matomo(Piwik)安装说明 安装前环境检查 Piwik要求PHP版本高于PHP5.5(选用PHP7.2) Piwik需要pdo和pdo_mysql或mysqli支持(选用mysqli) Piw ...

  7. Hibernate与 MyBatis的区别

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  8. java static在成员方法中

    package java08; public class Myclass { int num;//成员变量 static int numStatic;//静态变量 //成员方法 public void ...

  9. java通过反射拿到mybatis中的sql语句并操作

    private static final int MaxBatchLength = 100; public void updateBatch(List<T>list, BaseMapper ...

  10. Windows 搭建MongoDB分片集群(一)

    一.角色说明 要构建一个MongoDB分片集群,需要三个角色: shard server  即存储实际数据得分片,每个shard 可以是一个Mongod实例,也可以是一组mongod实例构成得Repl ...