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. Solr的学习使用之(二)schema.xml等配置文件的解析

    上一篇文章已经讲解了如何部署Solr,部署是部署完了,可是总觉得心里空空的,没底,里面有N多配置文件,比如schema.xml.solrConfig.xml.solr.xml and so on……都 ...

  2. USTC现代软件工程--summary

    起笔:我希望先简单总结一下我在这门课程中经历的一些工作以及学习到的一些东西,再对自己.队友.老师做一个评价.然后我想提出一些对这门课程的一些看法和建议,与自己的心得体会. 第一部分: 我在这门课上经历 ...

  3. 六、实现一个小功能 todolist

    1.创建一个新的Compnent 如果是通过 cli 创建的会自动加入,如果是手动创建的,需要自己加入. 2.实现添加效果 3.实现删除按钮 4.优化,把点击 添加 改为 回车 添加 5.优化,分成“ ...

  4. List接口和Set接口及其常用实现类概述

    一.List接口 List:有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元 ...

  5. 刷题or源码链接

    Hadoop权威指南的Github https://github.com/tomwhite/hadoop-book hadoopAPI http://hadoop.apache.org/docs/cu ...

  6. SpringBoot---常规属性配置

    1.概述 1.1.在Spring环境下,注入properties文件中的值,通过@PropertySource指明properties文件的位置,然后通过@Value注入值: 在SpringBoot环 ...

  7. Vue.js----date与时间戳的转换(unixTime)Moment.js让日期处理变得更简单

    当前日期格式化 let curTime = moment().format('YYYY-MM-DD HH:mm:ss') console.log('当前日期时间curTime:' + curTime) ...

  8. rem字体+百分比布局表格

    效果图: 上源码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. C++ 递推法 斐波那契数列 兔子产仔

    #include "stdio.h" #include "iostream" int Fibonacci(int n) { int t1, t2; || n = ...

  10. JS中的setTimeout()函数

    1.setTimeout() 方法 setTimeout() 方法用于在指定的毫秒数后调用函数或执行表达式.返回一个 ID(数字),可以将这个ID传递给 clearTimeout() 来取消执行. s ...