Leetcode 322. Coin Change

Source: Internet
Author: User

Transmission Door

322. Coin ChangeMy SubmissionsQuestionEditorial SolutionTotal accepted:20289 Total submissions:81565 difficulty:medium

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 ProgrammingTurn the key: http://blog.csdn.net/bruce128/article/details/50909889 given a few fixed-value coins, can be unlimited use. A target number that requires a minimum of coins to redeem the target.

A different way of thinking to understand the topic, each time you can walk a given face value of the number of steps, ask at least how many steps to achieve the goal. This can then be solved with BFS.

The second solution is dp,dp[i] = min {dp[i-a], dp[i-b], dp[i-c] ...}. Dynamic programming can be solved quickly as long as there is a formula.

I use DP to solve the AC code

180/180 Test cases passed. status:accepted
Runtime:76 ms

1 #defineINF 0X3FFFFFFF2 #defineN 10000053 4 intDp[n];5 intLen;6 7 classSolution {8  Public:9     intCoinchange (vector<int>& coins,intamount) {TenLen =coins.size (); One sort (Coins.begin (), Coins.end ()); A         inti,j; -         //Fill (DP,DP + n,inf); -          for(i =0; I <= amount;i++){ theDp[i] =inf; -         } -dp[0] =0; -          +          for(i =0; I < len;i++){ -              for(j =0; J < amount;j++){ +                 if(Dp[j] = = INF)Continue; ADp[j + coins[i]] = min (dp[j + coins[i]],dp[j] +1); at             } -         } -         if(Dp[amount] = = INF)return-1; -         returnDp[amount]; -     } -};

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