LeetCode.322(518) Coin Change && Coin Change2
2017-12-02 22:01
399 查看
题目322:
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
分析:class Solution {
public int coinChange(int[] coins, int amount) {
//给定货币种类和面额,以及需要求和的amount,返回满足该amount的组合最小长度。否则返回-1
//典型DP问题
if (amount==0) return 0;
if (coins.length==0) return -1;
int[] dp = new int[amount+1];
for (int i=1; i<=amount; i++) {
int min = amount+1;
for(int j=0;j<coins.length;j++){
//剩余值
int remain=i-coins[j];
if(remain>=0){
min=Math.min(min,dp[remain]+1);
}
}
dp[i]=min;
}
return (dp[amount] > amount) ? -1 : dp[amount];
}
}题目518:
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
Note: You can assume that
0 <= amount <= 5000
1 <= coin <= 5000
the number of coins is less than 500
the answer is guaranteed to fit into signed 32-bit integer
Example 1:Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.
Example 3:Input: amount = 10, coins = [10]
Output: 1
分析:public int change(int amount,int [] coins){
//给定金额总数,和零钱数组,求其组合总和
//思路:类似剪绳子,先求出公共最小的元素,之后求解大的元素
int [] dp=new int[amount+1];
dp[0]=1;
for(int i=0;i<coins.length;i++){
for(int j=coins[i];j<=amount;j++){
dp[j]+=dp[j-coins[i]];
}
}
return dp[amount];
}
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.
Note:
You may assume that you have an infinite number of each kind of coin.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
分析:class Solution {
public int coinChange(int[] coins, int amount) {
//给定货币种类和面额,以及需要求和的amount,返回满足该amount的组合最小长度。否则返回-1
//典型DP问题
if (amount==0) return 0;
if (coins.length==0) return -1;
int[] dp = new int[amount+1];
for (int i=1; i<=amount; i++) {
int min = amount+1;
for(int j=0;j<coins.length;j++){
//剩余值
int remain=i-coins[j];
if(remain>=0){
min=Math.min(min,dp[remain]+1);
}
}
dp[i]=min;
}
return (dp[amount] > amount) ? -1 : dp[amount];
}
}题目518:
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
Note: You can assume that
0 <= amount <= 5000
1 <= coin <= 5000
the number of coins is less than 500
the answer is guaranteed to fit into signed 32-bit integer
Example 1:Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.
Example 3:Input: amount = 10, coins = [10]
Output: 1
分析:public int change(int amount,int [] coins){
//给定金额总数,和零钱数组,求其组合总和
//思路:类似剪绳子,先求出公共最小的元素,之后求解大的元素
int [] dp=new int[amount+1];
dp[0]=1;
for(int i=0;i<coins.length;i++){
for(int j=coins[i];j<=amount;j++){
dp[j]+=dp[j-coins[i]];
}
}
return dp[amount];
}
相关文章推荐
- leetcode322 coin change & leetcode343 integer break
- DP问题:leetcode(322) Coin Change
- Leetcode (322) Coin Change
- Leetcode 322 & 518
- LeetCode 322 Coin Change (完全背包)
- 322 LeetCode Coin Change
- Leetcode 322 & 518
- 322. Coin Change
- LeetCode "Coin Change"
- 322. Coin Change
- leetcode之Coin Change
- 322. Coin Change
- 322. Coin Change
- 322. Coin Change
- 322. Coin Change
- Leetcode之CoinChange
- 322. Coin Change
- 322. Coin Change
- 322. Coin Change
- 322. Coin Change