53.Coin Change(找硬币)
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(找硬币)的更多相关文章
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- UVA 674 Coin Change 换硬币 经典dp入门题
题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数. 经典的dp题...可以递推也可以记忆化搜索... 我个人比较喜欢记忆化搜索,递推不是很熟练. 记忆化搜索:很白 ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
随机推荐
- jquery+ajax获取本地json对应数据
首先,记得导入jquery.js文件. json内容: var obj123=[ {"option":"2,3,9,14,19,24,32",&q ...
- Java JNA (一)—— 调用dll
Java调用C++动态链接库的方式很多,有jnative,jna等.这里介绍如何通过jna的方式调用动态链接库. 调用代码很简单,就是需要注意几个问题. 补充:如dll内部访问配置文件,需将配置文件放 ...
- Linux就该这么学07学习笔记
参考链接:https://www.linuxprobe.com/chapter-07.html RAID磁盘冗余阵列 RAID 0 RAID 0技术把多块物理硬盘设备(至少两块)通过硬件或软件的方式串 ...
- Sass函数:Introspection 函数 -type-of()
type-of() 函数主要用来判断一个值是属于什么类型: 返回值: number 为数值型. string 为字符串型. bool 为布尔型. color 为颜色型. >> type-o ...
- Ansible用法playbook
playbook文件 hello.yml --- - name: test_tasks [各个任务的总描述] hosts: webserver remote_user: root gather_fac ...
- 【归纳】Layui table.render里的json后台传入
在使用Layui的table元素时,传入的json的数据格式是有其自身定义的,需要另外添加一些字符,以正确传入. 为了传入符合前端格式的数据: table.render({ elem: '#test' ...
- Linux的运行级别和设置开机启动服务的方式
Linux的运行级别 什么是运行级别呢?简单点来说,运行级别就是操作系统当前正在运行的功能级别.级别是从0到6,具有不同的功能.这些级别定义在/ect/inittab文件中.这个文件是init程序寻找 ...
- JDBC简单总结
几种常用数据库的JDBC URL 对于 Oracle 数据库连接,采用如下形式: jdbc:oracle:thin:@localhost:1521:sid 对于 SQLServer 数据库连接,采用如 ...
- js中数组Array 详解
unshift:将参数添加到原数组开头,并返回数组的长度 pop:删除原数组最后一项,并返回删除元素的值:如果数组为空则返回undefined push:将参数添加到原数组末尾,并返回数组 ...
- instanceof是Java的一个二元操作符(运算符)
instanceof是Java的一个二元操作符(运算符),也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实例,返回的是boolean类型的数据.用它来判断某个对象是否是某个Clas ...