{"id":9452,"date":"2022-01-30T18:45:51","date_gmt":"2022-01-31T02:45:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9452"},"modified":"2022-01-31T23:30:35","modified_gmt":"2022-02-01T07:30:35","slug":"leetcode-2144-minimum-cost-of-buying-candies-with-discount","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2144-minimum-cost-of-buying-candies-with-discount\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2144. Minimum Cost of Buying Candies With Discount"},"content":{"rendered":"\n<p>A shop is selling candies at a discount. For&nbsp;<strong>every two<\/strong>&nbsp;candies sold, the shop gives a&nbsp;<strong>third<\/strong>&nbsp;candy for&nbsp;<strong>free<\/strong>.<\/p>\n\n\n\n<p>The customer can choose&nbsp;<strong>any<\/strong>&nbsp;candy to take away for free as long as the cost of the chosen candy is less than or equal to the&nbsp;<strong>minimum<\/strong>&nbsp;cost of the two candies bought.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if there are&nbsp;<code>4<\/code>&nbsp;candies with costs&nbsp;<code>1<\/code>,&nbsp;<code>2<\/code>,&nbsp;<code>3<\/code>, and&nbsp;<code>4<\/code>, and the customer buys candies with costs&nbsp;<code>2<\/code>&nbsp;and&nbsp;<code>3<\/code>, they&nbsp;can take the candy with cost&nbsp;<code>1<\/code>&nbsp;for free, but not the candy with cost&nbsp;<code>4<\/code>.<\/li><\/ul>\n\n\n\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>cost<\/code>, where&nbsp;<code>cost[i]<\/code>&nbsp;denotes the cost of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;candy, return&nbsp;<em>the&nbsp;<strong>minimum cost<\/strong>&nbsp;of buying&nbsp;<strong>all<\/strong>&nbsp;the candies<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cost = [1,2,3]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.\nThe total cost of buying all candies is 2 + 3 = 5. This is the <strong>only<\/strong> way we can buy the candies.\nNote that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.\nThe cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cost = [6,5,7,9,2,2]\n<strong>Output:<\/strong> 23\n<strong>Explanation:<\/strong> The way in which we can get the minimum cost is described below:\n- Buy candies with costs 9 and 7\n- Take the candy with cost 6 for free\n- We buy candies with costs 5 and 2\n- Take the last remaining candy with cost 2 for free\nHence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cost = [5,5]\n<strong>Output:<\/strong> 10\n<strong>Explanation:<\/strong> Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.\nHence, the minimum cost to buy all candies is 5 + 5 = 10.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= cost.length &lt;= 100<\/code><\/li><li><code>1 &lt;= cost[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort candies in descending order. Buy 1st, 2nd, take 3rd, buy 4th, 5th take 6th, &#8230;<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumCost(vector<int>& cost) {\n    sort(rbegin(cost), rend(cost));\n    while (cost.size() % 3) cost.push_back(0);\n    int ans = 0;\n    for (int i = 0; i < cost.size(); i += 3)\n      ans += cost[i] + cost[i + 1];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A shop is selling candies at a discount. For&nbsp;every two&nbsp;candies sold, the shop gives a&nbsp;third&nbsp;candy for&nbsp;free. The customer can choose&nbsp;any&nbsp;candy to take away for free&#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],"tags":[20,222,88],"class_list":["post-9452","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-array","tag-easy","tag-greedy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9452","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=9452"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9452\/revisions"}],"predecessor-version":[{"id":9457,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9452\/revisions\/9457"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}