{"id":6512,"date":"2020-03-21T01:05:42","date_gmt":"2020-03-21T08:05:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6512"},"modified":"2020-03-21T01:14:47","modified_gmt":"2020-03-21T08:14:47","slug":"leetcode-1383-maximum-performance-of-a-team","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1383-maximum-performance-of-a-team\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1383. Maximum Performance of a Team"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;engineers numbered from 1 to&nbsp;<code>n<\/code>&nbsp;and&nbsp;two arrays:&nbsp;<code>speed<\/code>&nbsp;and&nbsp;<code>efficiency<\/code>, where&nbsp;<code>speed[i]<\/code>&nbsp;and&nbsp;<code>efficiency[i]<\/code>&nbsp;represent the speed and efficiency for the i-th engineer respectively.&nbsp;<em>Return the maximum&nbsp;<strong>performance<\/strong>&nbsp;of a team composed of&nbsp;at most&nbsp;<code>k<\/code>&nbsp;engineers, since the answer can be a huge number, return this modulo&nbsp;10^9 + 7.<\/em><\/p>\n\n\n\n<p>The&nbsp;<strong>performance<\/strong>&nbsp;of a team is the sum of their engineers&#8217; speeds multiplied by the minimum efficiency among&nbsp;their engineers.&nbsp;<\/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> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2\n<strong>Output:<\/strong> 60\n<strong>Explanation:<\/strong> \nWe have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.\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> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3\n<strong>Output:<\/strong> 68\n<strong>Explanation:\n<\/strong>This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.\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> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4\n<strong>Output:<\/strong> 72\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^5<\/code><\/li><li><code>speed.length == n<\/code><\/li><li><code>efficiency.length == n<\/code><\/li><li><code>1 &lt;= speed[i] &lt;= 10^5<\/code><\/li><li><code>1 &lt;= efficiency[i] &lt;= 10^8<\/code><\/li><li><code>1 &lt;= k &lt;= n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong> + <strong>Sliding Window<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Sort engineers by their efficiency in descending order.<\/li><li>For each window of K engineers (we can have less than K people in the first k-1 windows), ans is sum(speed) * min(efficiency).<\/li><\/ol>\n\n\n\n<p>Time complexity: O(nlogn) + O(nlogk)<br>Space complexity: O(n)<\/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 maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {\n    vector<pair<int, int>> es;\n    for (int i = 0; i < n; ++i)\n      es.push_back({efficiency[i], speed[i]});\n    sort(rbegin(es), rend(es));\n    priority_queue<int, vector<int>, greater<int>> q;\n    long sum = 0;\n    long ans = 0;\n    for (int i = 0; i < n; ++i) {\n      if (i >= k) {\n        sum -= q.top();\n        q.pop();\n      }\n      sum += es[i].second;\n      q.push(es[i].second);\n      ans = max(ans, sum * es[i].first);\n    }\n    return ans % static_cast<int>(1e9 + 7);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:\n    speeds = 0\n    ans = 0\n    q = []\n    for e, s in sorted(zip(efficiency, speed), reverse=True):\n      if len(q) >= k:\n        speeds -= heapq.heappop(q)\n      speeds += s\n      heapq.heappush(q, s)\n      ans = max(ans, speeds * e)\n    return ans % (10**9 + 7)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;engineers numbered from 1 to&nbsp;n&nbsp;and&nbsp;two arrays:&nbsp;speed&nbsp;and&nbsp;efficiency, where&nbsp;speed[i]&nbsp;and&nbsp;efficiency[i]&nbsp;represent the speed and efficiency for the i-th engineer respectively.&nbsp;Return the maximum&nbsp;performance&nbsp;of a team composed of&nbsp;at most&nbsp;k&nbsp;engineers, since&#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,217,327,215],"class_list":["post-6512","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hard","tag-k","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6512","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=6512"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6512\/revisions"}],"predecessor-version":[{"id":6515,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6512\/revisions\/6515"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}