{"id":7622,"date":"2020-11-07T23:12:01","date_gmt":"2020-11-08T07:12:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7622"},"modified":"2020-11-08T09:27:33","modified_gmt":"2020-11-08T17:27:33","slug":"leetcode-1648-sell-diminishing-valued-colored-balls","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1648-sell-diminishing-valued-colored-balls\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1648. Sell Diminishing-Valued Colored Balls"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1648. Sell Diminishing-Valued Colored Balls - \u5237\u9898\u627e\u5de5\u4f5c EP368\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/kBxqAnWo9Wo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You have an&nbsp;<code>inventory<\/code>&nbsp;of different colored balls, and there is a customer that wants&nbsp;<code>orders<\/code>&nbsp;balls of&nbsp;<strong>any<\/strong>&nbsp;color.<\/p>\n\n\n\n<p>The customer weirdly values the colored balls. Each colored ball&#8217;s value is the number of balls&nbsp;<strong>of that color&nbsp;<\/strong>you currently have in your&nbsp;<code>inventory<\/code>. For example, if you own&nbsp;<code>6<\/code>&nbsp;yellow balls, the customer would pay&nbsp;<code>6<\/code>&nbsp;for the first yellow ball. After the transaction, there are only&nbsp;<code>5<\/code>&nbsp;yellow balls left, so the next yellow ball is then valued at&nbsp;<code>5<\/code>&nbsp;(i.e., the value of the balls decreases as you sell more to the customer).<\/p>\n\n\n\n<p>You are given an integer array,&nbsp;<code>inventory<\/code>, where&nbsp;<code>inventory[i]<\/code>&nbsp;represents the number of balls of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;color that you initially own. You are also given an integer&nbsp;<code>orders<\/code>, which represents the total number of balls that the customer wants. You can sell the balls&nbsp;<strong>in any order<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;total value that you can attain after selling&nbsp;<\/em><code>orders<\/code><em>&nbsp;colored balls<\/em>. As the answer may be too large, return it&nbsp;<strong>modulo&nbsp;<\/strong><code>10<sup>9&nbsp;<\/sup>+ 7<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/05\/jj.gif\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> inventory = [2,5], orders = 4\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong> Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3).\nThe maximum total value is 2 + 5 + 4 + 3 = 14.\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> inventory = [3,5], orders = 6\n<strong>Output:<\/strong> 19\n<strong>Explanation: <\/strong>Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2).\nThe maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.\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> inventory = [2,8,4,10,6], orders = 20\n<strong>Output:<\/strong> 110\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> inventory = [1000000000], orders = 1000000000\n<strong>Output:<\/strong> 21\n<strong>Explanation: <\/strong>Sell the 1st color 1000000000 times for a total value of 500000000500000000. 500000000500000000 modulo 10<sup>9 <\/sup>+ 7 = 21.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= inventory.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= inventory[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= orders &lt;= min(sum(inventory[i]), 10<sup>9<\/sup>)<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-1.png\" alt=\"\" class=\"wp-image-7630\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-2.png\" alt=\"\" class=\"wp-image-7631\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/11\/1648-ep368-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>Sort the colors by # of balls in descending order.<br>e.g. 3 7 5 1 =&gt; 7 5 3 1<\/li><li>Sell the color with largest number of balls until it has the same number of balls of next color<ol><li>7 5 3 1 =&gt; 6 5 3 1 =&gt; 5 5 3 1 # value = 7 + 6 = 13<\/li><li>5 5 3 1 =&gt; 4 4 3 1 =&gt; 3 3 3 1 # value = 13 + (5 + 4) * 2 = 31<\/li><li>3 3 3 1 =&gt; 2 2 2 1 =&gt; 1 1 1 1 # value = 31 + (3 + 2) * 3 = 46<\/li><li>1 1 1 1 =&gt; 0 0 0 0 # value = 46 + 1 * 4 = 50<\/li><\/ol><\/li><li>Need to handle the case if orders &lt; total balls&#8230;<br><\/li><\/ol>\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 maxProfit(vector<int>& inventory, int orders) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = inventory.size();\n    sort(rbegin(inventory), rend(inventory));\n    long cur = inventory[0];\n    long ans = 0;\n    int c = 0;\n    while (orders) {      \n      while (c < n &#038;&#038; inventory[c] == cur) ++c;\n      int nxt = c == n ? 0 : inventory[c];      \n      int count = min(static_cast<long>(orders), c * (cur - nxt));\n      int t = cur - nxt;\n      int r = 0;\n      if (orders < c * (cur - nxt)) {\n        t = orders \/ c;\n        r = orders % c;\n      }\n      ans = (ans + (cur + cur - t + 1) * t \/ 2 * c + (cur - t) * r) % kMod;\n      orders -= count;\n      cur = nxt;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have an&nbsp;inventory&nbsp;of different colored balls, and there is a customer that wants&nbsp;orders&nbsp;balls of&nbsp;any&nbsp;color. The customer weirdly values the colored balls. Each colored ball&#8217;s value&#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":[88,31,62],"class_list":["post-7622","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-math","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7622","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=7622"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7622\/revisions"}],"predecessor-version":[{"id":7633,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7622\/revisions\/7633"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}