{"id":2969,"date":"2018-06-30T22:46:37","date_gmt":"2018-07-01T05:46:37","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2969"},"modified":"2018-06-30T22:49:50","modified_gmt":"2018-07-01T05:49:50","slug":"leetcode-860-lemonade-change","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-860-lemonade-change\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 860. Lemonade Change"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>At a lemonade stand, each lemonade costs\u00a0<code>$5<\/code>.<\/p>\n<p>Customers are standing in a queue to buy from you, and order one at a time (in the order specified by\u00a0<code>bills<\/code>).<\/p>\n<p>Each customer will only buy one lemonade and\u00a0pay with either a\u00a0<code>$5<\/code>,\u00a0<code>$10<\/code>, or\u00a0<code>$20<\/code>\u00a0bill.\u00a0 You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.<\/p>\n<p>Note that you don&#8217;t have any change\u00a0in hand at first.<\/p>\n<p>Return\u00a0<code>true<\/code>\u00a0if and only if you can provide every customer with correct change.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[5,5,5,10,20]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">true<\/span>\r\n<strong>Explanation: <\/strong>\r\nFrom the first 3 customers, we collect three $5 bills in order.\r\nFrom the fourth customer, we collect a $10 bill and give back a $5.\r\nFrom the fifth customer, we give a $10 bill and a $5 bill.\r\nSince all customers got correct change, we output true.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong><span id=\"example-input-2-1\">[5,5,10]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">true<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">[10,10]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">false<\/span>\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-4-1\">[5,5,10,10,20]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">false<\/span>\r\n<strong>Explanation: <\/strong>\r\nFrom the first two customers in order, we collect two $5 bills.\r\nFor the next two customers in order, we collect a $10 bill and give back a $5 bill.\r\nFor the last customer, we can't give change of $15 back because we only have two $10 bills.\r\nSince not every customer received correct change, the answer is false.\r\n<\/pre>\n<h1><strong>Solution: Simulation + Greedy<\/strong><\/h1>\n<p>Always use 10 bill first.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 18 ms\r\nclass Solution {\r\npublic:\r\n  bool lemonadeChange(vector&lt;int&gt;&amp; bills) {\r\n    int fives = 0;\r\n    int tens = 0;\r\n    for (const int bill : bills) {\r\n      if (bill == 5) {\r\n        ++fives;\r\n      } else if (bill == 10) {\r\n        if (!fives) return false;\r\n        ++tens;\r\n        --fives;\r\n      } else if (bill == 20) {\r\n        if (tens &amp;&amp; fives) {\r\n          --tens;\r\n          --fives;\r\n        } else if (fives &gt;= 3) {\r\n          fives -= 3;\r\n        } else {\r\n          return false;\r\n        }\r\n      }\r\n    }\r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem At a lemonade stand, each lemonade costs\u00a0$5. Customers are standing in a queue to buy from you, and order one at a time (in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,48],"tags":[88,179],"class_list":["post-2969","post","type-post","status-publish","format-standard","hentry","category-greedy","category-simulation","tag-greedy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2969"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2969\/revisions"}],"predecessor-version":[{"id":2971,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2969\/revisions\/2971"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}