{"id":9679,"date":"2022-04-17T16:07:25","date_gmt":"2022-04-17T23:07:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9679"},"modified":"2022-04-17T16:08:59","modified_gmt":"2022-04-17T23:08:59","slug":"leetcode-2244-minimum-rounds-to-complete-all-tasks","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2244-minimum-rounds-to-complete-all-tasks\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2244. Minimum Rounds to Complete All Tasks"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>tasks<\/code>, where&nbsp;<code>tasks[i]<\/code>&nbsp;represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the&nbsp;<strong>same difficulty level<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;rounds required to complete all the tasks, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it is not possible to complete all the tasks.<\/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 = [2,2,3,3,2,4,4,4,4,4]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> To complete all the tasks, a possible plan is:\n- In the first round, you complete 3 tasks of difficulty level 2. \n- In the second round, you complete 2 tasks of difficulty level 3. \n- In the third round, you complete 3 tasks of difficulty level 4. \n- In the fourth round, you complete 2 tasks of difficulty level 4.  \nIt can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.\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 = [2,3,3]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= tasks.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= tasks[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Count the frequency of each level. The only case that can not be finished is 1 task at some level. Otherwise we can always finish it by 2, 3 tasks at a time. <\/p>\n\n\n\n<p>if n = 2: 2 => 1 round<br><meta charset=\"utf-8\">if n = 3: 3 => 1 <meta charset=\"utf-8\">round<br><meta charset=\"utf-8\">if n = 4: 2 + 2 => 2 <meta charset=\"utf-8\">rounds<br><meta charset=\"utf-8\">if n = 5: 3 + 2 => 2 <meta charset=\"utf-8\">rounds<br>&#8230;<br><meta charset=\"utf-8\">if n = 3k, n % 3 == 0 : 3 + 3 + &#8230; + 3 = k rounds<br><meta charset=\"utf-8\">if n = 3k + 1,  n % 3 == 1 : 3*(k &#8211; 1) + 2 + 2 = k + 1 rounds<br><meta charset=\"utf-8\">if n = 3k + 2, n % 3 == 2 : 3*k + 2 = k + 1 rounds<\/p>\n\n\n\n<p>We need (n + 2) \/ 3 rounds. <\/p>\n\n\n\n<p>Time complexity: O(n)<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 minimumRounds(vector<int>& tasks) {\n    unordered_map<int, int> m;\n    for (int t : tasks) ++m[t];\n    int ans = 0;\n    for (auto [level, count] : m) {\n      if (count == 1) return -1;\n      ans += (count + 2) \/ 3;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;tasks, where&nbsp;tasks[i]&nbsp;represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the&nbsp;same&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[82,31,177],"class_list":["post-9679","post","type-post","status-publish","format-standard","hentry","category-math","tag-hashtable","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9679","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=9679"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9679\/revisions"}],"predecessor-version":[{"id":9683,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9679\/revisions\/9683"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}