动态规划——322. Coin Change
2016-07-05 10:50
399 查看
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
Example 1:
coins =
return
Example 2:
coins =
return
Note:
You may assume that you have an infinite number of each kind of coin.
d[i]表示 凑到i数值所用的最小硬币数
递推公式
d[i]=min(d[i],d[i-coins[j]]+1)
i时 等于 i-coins[j] +1的最小值,为了方便比较将所有的d[i] 初始化为最大值 0x7ffffffe
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.
Note:
You may assume that you have an infinite number of each kind of coin.
d[i]表示 凑到i数值所用的最小硬币数
递推公式
d[i]=min(d[i],d[i-coins[j]]+1)
i时 等于 i-coins[j] +1的最小值,为了方便比较将所有的d[i] 初始化为最大值 0x7ffffffe
public class Solution { public int coinChange(int[] coins, int amount) { int [] d=new int[amount+1]; d[0]=0; final int INF = 0x7ffffffe; for (int i = 1; i <= amount; i++) d[i] = INF; for(int i=1;iamount?-1:d[amount]; } }
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- C#使用动态规划解决0-1背包问题实例分析
- 动态规划
- C++ 动态规划
- 动态规划解决背包问题的核心思路
- DP(动态规划) 解游轮费用问题
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划
- 自顶向下动态规划解决最长公共子序列(LCS)问题
- 01背包问题
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 关于爬楼梯的动态规划算法
- 动态规划 --- hdu 1003 **
- DP问题各种模型的状态转移方程
- 0-1背包解题过程
- 背包问题