{"id":1971,"date":"2016-04-11T18:01:57","date_gmt":"2016-04-11T10:01:57","guid":{"rendered":"http:\/\/boweihe.me\/?p=1971"},"modified":"2016-04-11T18:01:57","modified_gmt":"2016-04-11T10:01:57","slug":"leetcode-322-coin-change","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1971","title":{"rendered":"LeetCode 322. Coin Change"},"content":{"rendered":"<div class=\"question-title\"><\/div>\n<div class=\"row\">\n<div class=\"col-md-12\">\n<div class=\"question-content\">\nYou are given coins of different denominations and a total amount of money <i>amount<\/i>. 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 <code>-1<\/code>.<br \/>\n<b>Example 1:<\/b><br \/>\ncoins = <code>[1, 2, 5]<\/code>, amount = <code>11<\/code><br \/>\nreturn <code>3<\/code> (11 = 5 + 5 + 1)<br \/>\n<b>Example 2:<\/b><br \/>\ncoins = <code>[2]<\/code>, amount = <code>3<\/code><br \/>\nreturn <code>-1<\/code>.<br \/>\n<b>Note<\/b>:<br \/>\nYou may assume that you have an infinite number of each kind of coin.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u52a8\u6001\u89c4\u5212\u7684\u95ee\u9898\uff0c\u4e0d\u8fc7\u5982\u679c\u5012\u7740\u505a\u7684\u8bdd\uff0c\u4f3c\u4e4e\u4f1a\u8d85\u65f6&#8230;<br \/>\n\u6240\u4ee5\u8003\u8651\u5f80\u524d\u9012\u63a8\u7684\u60c5\u51b5\uff0c\u5373\u5f53\u524d\u8981\u89e3\u51b3X\u5143\u7684\u627e\u96f6\u95ee\u9898\u7684\u6700\u4f73\u89e3\u6cd5\u4e14\u5df2\u77e5&lt;X\u65f6\u7684\u89e3\u6cd5\uff0c\u624b\u5934\u6709\u4e00\u5806\u9762\u503c\u4e3acoins[i]\u7684\u786c\u5e01\u7684\u65f6\u5019\uff0c\u5bf9\u4e8e\u6bcf\u79cd\u53ef\u7528\u7684\u786c\u5e01\u6211\u4eec\u8981\u505a\u7684\u9009\u62e9\u662f\uff1a<\/p>\n<ul>\n<li>\u5c1d\u8bd5\u4f7f\u7528coin[i]\uff0c\u5373\u5f53\u524d\u7684\u6700\u4f18\u7ed3\u679c=(X-coins[i])\u5143\u65f6\u7684\u6700\u4f18\u7ed3\u679c+1\uff0c\u8fd9\u4e2a1\u5c31\u662f\u7528\u4e86coins[i]\u8fd9\u4e2a\u786c\u5e01\uff1b<\/li>\n<li>\u4fdd\u6301\u73b0\u6709\u7ed3\u679c\u4e0d\u53d8<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5f53\u7136\u662f\u53d6\u6700\u597d\u7684\u5b58\u4e0b\u6765\u4e86\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int MAX_NUM = 65535;\n    int coinChange(vector&lt;int&gt;&amp; coins, int amount) {\n        vector&lt;int&gt; solutions(amount+1, MAX_NUM); \/\/0~amount\uff0c\u5171amount+1\u4e2a\n        solutions[0] = 0;\n        int coinSz = coins.size();\n        for(int i=1; i&lt;=amount; i++){\n            for(int c=0; c&lt;coinSz; c++){\n                \/\/\u5f97\u5230\u5f53\u524d\u7684amount\uff0c\u8981\u4e48\u5c31\u662f\u7528\u539f\u6709\u7684\u7ed3\u679c\uff0c\u8981\u4e48\u5c31\u662f\u4f7f\u7528amount[i-coin] + 1\u4e2a\u5f53\u524d\u9762\u503c\u7684\n                if(coins[c] &lt;= i){\n                    solutions[i] = min(solutions[i], 1 + solutions[i-coins[c]]);\n                }\n            }\n        }\n        if(solutions[amount] == MAX_NUM)\n            return -1;\n        return solutions[amount];\n    }\n};<\/pre>\n<p>&nbsp;\n<\/p><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[65,66,141],"class_list":["post-1971","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode","tag-leetcode-oj","tag-141"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1971"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1971\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}