{"id":6572,"date":"2020-04-04T21:14:56","date_gmt":"2020-04-05T04:14:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6572"},"modified":"2020-04-04T21:15:32","modified_gmt":"2020-04-05T04:15:32","slug":"leetcode-1402-reducing-dishes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1402-reducing-dishes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1402. Reducing Dishes"},"content":{"rendered":"\n<p>A chef&nbsp;has collected data on the&nbsp;<code>satisfaction<\/code>&nbsp;level of his&nbsp;<code>n<\/code>&nbsp;dishes.&nbsp;Chef can cook any dish in 1 unit of time.<\/p>\n\n\n\n<p><em>Like-time coefficient<\/em>&nbsp;of a dish is defined as&nbsp;the time taken to cook that dish including previous dishes multiplied by its satisfaction level &nbsp;i.e.&nbsp;&nbsp;<code>time[i]<\/code>*<code>satisfaction[i]<\/code><\/p>\n\n\n\n<p>Return&nbsp;the maximum sum of&nbsp;<em>Like-time coefficient&nbsp;<\/em>that the chef can obtain after dishes preparation.<\/p>\n\n\n\n<p>Dishes can be prepared in&nbsp;<strong>any&nbsp;<\/strong>order and the chef can discard some dishes to get this maximum value.<\/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> satisfaction = [-1,-8,0,5,-9]\n<strong>Output:<\/strong> 14\n<strong>Explanation: <\/strong>After Removing the second and last dish, the maximum total <em>Like-time coefficient<\/em> will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.<\/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> satisfaction = [4,3,2]\n<strong>Output:<\/strong> 20\n<strong>Explanation:<\/strong> Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)\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> satisfaction = [-1,-4,-5]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> People don't like the dishes. No dish is prepared.\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> satisfaction = [-2,5,-1,0,3,-3]\n<strong>Output:<\/strong> 35\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == satisfaction.length<\/code><\/li><li><code>1 &lt;= n &lt;= 500<\/code><\/li><li><code>-10^3 &lt;=&nbsp;satisfaction[i] &lt;= 10^3<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Sort<\/strong> <strong>+ Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn + n^2)<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 maxSatisfaction(vector<int>& s) {\n    const int n = s.size();\n    sort(begin(s), end(s));\n    int ans = 0;\n    for (int i = 0; i < n; ++i) {\n      int v = 0;\n      for (int j = i; j < n; ++j)\n        v += s[j] * (j - i + 1);\n      ans = max(ans, v);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Sort + prefix sum<\/strong><\/h2>\n\n\n\n<p>Sort in reverse order, accumulate prefix sum until prefix sum &lt;= 0.<\/p>\n\n\n\n<p>Time complexity: O(nlogn + n)<br>Space complexity: O(1)<\/p>\n\n\n\n<p>[9, 8, 5, 2, 1, -1]<br>sum = 9 * 4 + 8 * 3 + 2 * 3 + 1 * 2 + -1 * 1<br>&lt;=&gt;<br>sum += 9<br>sum += (9 + 8 = 17)<br>sum += (17 + 2 = 19)<br>sum += (19 + 1 = 20)<br>sum += (20 - 1 = 19)<\/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 maxSatisfaction(vector<int>& s) {\n    const int n = s.size();\n    sort(rbegin(s), rend(s));\n    int ans = 0;\n    int prefix = 0;\n    for (int v : s) {\n      prefix += v;\n      if (prefix <= 0) break;\n      ans += prefix;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A chef&nbsp;has collected data on the&nbsp;satisfaction&nbsp;level of his&nbsp;n&nbsp;dishes.&nbsp;Chef can cook any dish in 1 unit of time. Like-time coefficient&nbsp;of a dish is defined as&nbsp;the time&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,23],"class_list":["post-6572","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6572","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=6572"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6572\/revisions"}],"predecessor-version":[{"id":6574,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6572\/revisions\/6574"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}