322. 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.

Subscribe to see which companies asked this question

Hide TagsDynamic Programming

Dp:constructing the solution in the order of sum. (Get 1, 2, ..., amount)

classSolution { Public intCoinchange (int[] coins,intamount) {    if(Amount < 1)      return0; int[] DP =New int[Amount + 1]; intsum = 0;  while(++sum <=amount) {      //constructing the result from ..., amount      intmin =-1;//-1 means cannot get this amount.       for(intcoin:coins) {        if(Sum >= coin && dp[sum-coin]! =-1) {          intTempsolution = Dp[sum-coin] + 1; if(min = =-1) min= Tempsolution;//Initialize solution.          Elsemin=math.min (tempsolution, Min); }} Dp[sum]=min; }    returnDp[amount]; }}

Dp:constructing the solution by how many coins used. (use 1 coin, use 2 coins, ... until the target is reached.)

 Public classSolution { Public intCoinchange (int[] coins,intamount) {        if(Amount = = 0)            return0; intLen = amount+1; int[] result =New int[Len]; result[0] = 0; List<Integer> q =NewArraylist<integer>(); intMinimumcoins = 1;  for(Integer c:coins) {if(C <Len) {Result[c]=Minimumcoins;            Q.add (c); }        }                 while(!Q.isempty ()) {            ++Minimumcoins; if(Result[amount]! = 0)                returnResult[amount]; List<Integer> next =NewArraylist<integer>();  for(Integer c:coins) { for(Integer optimal:q) {intsum = c+Optimal; if(Sum < len && result[sum] = = 0)                    {                        //Find The new numbers so you can get a with minimumcoins coins. //Add One more coin to these numbers,//You can get the next group of those optimial numbers.Result[sum] =Minimumcoins;                    Next.add (sum); }}} Q=Next; }                return-1; }}

322. 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.