{"id":8702,"date":"2021-11-14T20:45:28","date_gmt":"2021-11-15T04:45:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8702"},"modified":"2021-11-14T20:49:18","modified_gmt":"2021-11-15T04:49:18","slug":"leetcode-2071-maximum-number-of-tasks-you-can-assign","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2071-maximum-number-of-tasks-you-can-assign\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2071. Maximum Number of Tasks You Can Assign"},"content":{"rendered":"\n<p>You have&nbsp;<code>n<\/code>&nbsp;tasks and&nbsp;<code>m<\/code>&nbsp;workers. Each task has a strength requirement stored in a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>tasks<\/code>, with the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;task requiring&nbsp;<code>tasks[i]<\/code>&nbsp;strength to complete. The strength of each worker is stored in a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>workers<\/code>, with the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;worker having&nbsp;<code>workers[j]<\/code>&nbsp;strength. Each worker can only be assigned to a&nbsp;<strong>single<\/strong>&nbsp;task and must have a strength&nbsp;<strong>greater than or equal<\/strong>&nbsp;to the task&#8217;s strength requirement (i.e.,&nbsp;<code>workers[j] &gt;= tasks[i]<\/code>).<\/p>\n\n\n\n<p>Additionally, you have&nbsp;<code>pills<\/code>&nbsp;magical pills that will&nbsp;<strong>increase a worker&#8217;s strength<\/strong>&nbsp;by&nbsp;<code>strength<\/code>. You can decide which workers receive the magical pills, however, you may only give each worker&nbsp;<strong>at most one<\/strong>&nbsp;magical pill.<\/p>\n\n\n\n<p>Given the&nbsp;<strong>0-indexed&nbsp;<\/strong>integer arrays&nbsp;<code>tasks<\/code>&nbsp;and&nbsp;<code>workers<\/code>&nbsp;and the integers&nbsp;<code>pills<\/code>&nbsp;and&nbsp;<code>strength<\/code>, return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of tasks that can be completed.<\/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> tasks = [<strong>3<\/strong>,<strong>2<\/strong>,<strong>1<\/strong>], workers = [<strong>0<\/strong>,<strong>3<\/strong>,<strong>3<\/strong>], pills = 1, strength = 1\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\nWe can assign the magical pill and tasks as follows:\n- Give the magical pill to worker 0.\n- Assign worker 0 to task 2 (0 + 1 &gt;= 1)\n- Assign worker 1 to task 1 (3 &gt;= 2)\n- Assign worker 2 to task 0 (3 &gt;= 3)\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> tasks = [<strong>5<\/strong>,4], workers = [<strong>0<\/strong>,0,0], pills = 1, strength = 5\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nWe can assign the magical pill and tasks as follows:\n- Give the magical pill to worker 0.\n- Assign worker 0 to task 0 (0 + 5 &gt;= 5)\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> tasks = [<strong>10<\/strong>,<strong>15<\/strong>,30], workers = [<strong>0<\/strong>,<strong>10<\/strong>,10,10,10], pills = 3, strength = 10\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nWe can assign the magical pills and tasks as follows:\n- Give the magical pill to worker 0 and worker 1.\n- Assign worker 0 to task 0 (0 + 10 &gt;= 10)\n- Assign worker 1 to task 1 (10 + 10 &gt;= 15)\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> tasks = [<strong>5<\/strong>,9,<strong>8<\/strong>,<strong>5<\/strong>,9], workers = [1,<strong><u>6<\/u><\/strong>,<strong>4<\/strong>,2,<strong>6<\/strong>], pills = 1, strength = 5\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\nWe can assign the magical pill and tasks as follows:\n- Give the magical pill to worker 2.\n- Assign worker 1 to task 0 (6 &gt;= 5)\n- Assign worker 2 to task 2 (4 + 5 &gt;= 8)\n- Assign worker 4 to task 3 (6 &gt;= 5)\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == tasks.length<\/code><\/li><li><code>m == workers.length<\/code><\/li><li><code>1 &lt;= n, m &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= pills &lt;= m<\/code><\/li><li><code>0 &lt;= tasks[i], workers[j], strength &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Binary Search in Binary Search.<\/strong><\/h2>\n\n\n\n<p>Find the smallest k, s.t. we are <strong>NOT<\/strong> able to assign. Then answer is k- 1.<\/p>\n\n\n\n<p>The key is to <strong>verify<\/strong> whether we can assign k tasks or not.<\/p>\n\n\n\n<p>Greedy: We want k smallest tasks and k strongest workers.<\/p>\n\n\n\n<p>Start with the hardest tasks among (smallest) k:<br>1. assign task[i] to the weakest worker without a pill (if he can handle the hardest work so far, then the stronger workers can handle any simpler tasks left)<br>2. If 1) is not possible, we find a weakest worker + pill that can handle task[i] (otherwise we are wasting workers)<br>3. If 2) is not possible, impossible to finish k tasks.<\/p>\n\n\n\n<p>Let k = min(n, m)<br>Time complexity: O((logk)<sup>2<\/sup> * k)<br>Space complexity: O(k)<\/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 maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {\n    const int n = tasks.size();    \n    sort(begin(tasks), end(tasks));    \n    sort(begin(workers), end(workers)); \n    auto check = [&](int k) {      \n      multiset<int> ws(rbegin(workers), rbegin(workers) + k);\n      for (int i = k - 1, p = 0; i >= 0; --i) {\n        auto it = ws.lower_bound(tasks[i]);\n        if (it != end(ws)) {\n          ws.erase(it);\n        } else if ((it = ws.lower_bound(tasks[i] - strength)) != end(ws)) {\n          if (++p > pills) return false;\n          ws.erase(it);\n        } else {\n          return false;\n        }\n      }\n      return true;\n    };\n    int l = 0;\n    int r = min(tasks.size(), workers.size()) + 1;\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      if (!check(m)) {\n        r = m;\n      } else {\n        l = m + 1;\n      }\n    }\n    return l - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have&nbsp;n&nbsp;tasks and&nbsp;m&nbsp;workers. Each task has a strength requirement stored in a&nbsp;0-indexed&nbsp;integer array&nbsp;tasks, with the&nbsp;ith&nbsp;task requiring&nbsp;tasks[i]&nbsp;strength to complete. The strength of each worker is stored&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,51],"tags":[52,217],"class_list":["post-8702","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-greedy","tag-binary-search","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8702","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=8702"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8702\/revisions"}],"predecessor-version":[{"id":8706,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8702\/revisions\/8706"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}