[Leetcode] Coin Change Coin Change

Source: Internet
Author: User

You is given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins so need to make up that amount. If that amount of cannot is 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 so you have a infinite number of each kind of coin.

Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.

This problem gave us some useful coin value, and gave a sum of money, asked us to use a few coins to change the minimum. According to the example of the topic, not every time will give full 1,2,5 coins, sometimes no 1 cents, then some money can not change, need to return-1. This problem with the Careercup on the road 9.8 represent N Cents cent composition somewhat similar, the problem to all the cents, 25,10,5,1, and then give us a sum of money, ask us all the way to change, and this problem only let us find the smallest kind, In order to find the extremum problem, we still consider the dynamic programming dynamically programming to do, we maintain a one-dimensional dynamic array dp, where Dp[i] represents the minimum number of coins when the money is I the change, the recursive type is:

Dp[i] = min (Dp[i], Dp[i-coins[j]] + 1);

Where Coins[j] is the first J-coin, and i-coins[j] is the sum of money I minus the value of one of the coins, the remaining amount of money in the DP array to find the value, and then add 1 and the current DP array to compare the value, take the smaller one update DP array. Let's look at the iterations as follows:

Solution One:

//non-recursionclassSolution { Public:    intCoinchange (vector<int>& coins,intamount) {Vector<int> dp (amount +1, Amount +1); dp[0] =0;  for(inti =1; I <= amount; ++i) { for(intj =0; J < Coins.size (); ++j) {if(Coins[j] <=i) {Dp[i]= Min (Dp[i], Dp[i-coins[j]] +1); }            }        }        returnDp[amount] > amount? -1: Dp[amount]; }};

Then look at recursion, the same way of thinking, just a few differences:

Solution Two:

//recursionclassSolution { Public:    intCoinchange (vector<int>& coins,intamount) {Vector<int> dp (amount +1, Int_max); dp[0] =0; returnCoinchangedfs (coins, amount, DP); }    intCoinchangedfs (vector<int> &coins,intAmount, vector<int> &DP) {        if(Amount <0)return-1; if(Dp[amount]! = Int_max)returnDp[amount];  for(inti =0; I < coins.size (); ++i) {intTMP = COINCHANGEDFS (coins, amount-Coins[i], DP); if(TMP >=0) Dp[amount] = min (Dp[amount], tmp +1); }        returnDp[amount] = dp[amount] = = Int_max? -1: Dp[amount]; }};

Finally, a recursive solution using a hash table:

Solution Three:

//recursionclassSolution { Public:    intCoinchange (vector<int>& coins,intamount) {Unordered_map<int,int>DP; dp[0] =0; returnCoinchangedfs (coins, amount, DP); }    intCoinchangedfs (vector<int> &coins,intAmount, unordered_map<int,int> &DP) {        if(Amount <0)return-1; if(Dp.find (amount)! = Dp.end ())returnDp[amount]; intCur =Int_max;  for(inti =0; I < coins.size (); ++i) {intTMP = COINCHANGEDFS (coins, amount-Coins[i], DP); if(TMP >=0) cur = min (cur, tmp +1); }        returnDp[amount] = cur = = Int_max? -1: cur; }};

Similar topics:

9.8 represent N Cents cents composition

Resources:

Https://leetcode.com/discuss/76194/c-o-n-amount-time-o-amount-space-dp-solution

Https://leetcode.com/discuss/76217/java-both-iterative-recursive-solutions-with-explanations

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Coin Change Coin Change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.