{"id":9323,"date":"2021-12-31T13:17:39","date_gmt":"2021-12-31T21:17:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9323"},"modified":"2021-12-31T13:20:17","modified_gmt":"2021-12-31T21:20:17","slug":"leetcode-1953-maximum-number-of-weeks-for-which-you-can-work","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1953-maximum-number-of-weeks-for-which-you-can-work\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1953. Maximum Number of Weeks for Which You Can Work"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;projects numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. You are given an integer array&nbsp;<code>milestones<\/code>&nbsp;where each&nbsp;<code>milestones[i]<\/code>&nbsp;denotes the number of milestones the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;project has.<\/p>\n\n\n\n<p>You can work on the projects following these two rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Every week, you will finish&nbsp;<strong>exactly one<\/strong>&nbsp;milestone of&nbsp;<strong>one<\/strong>&nbsp;project. You&nbsp;<strong>must<\/strong>&nbsp;work every week.<\/li><li>You&nbsp;<strong>cannot<\/strong>&nbsp;work on two milestones from the same project for two&nbsp;<strong>consecutive<\/strong>&nbsp;weeks.<\/li><\/ul>\n\n\n\n<p>Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will&nbsp;<strong>stop working<\/strong>. Note that you may not be able to finish every project&#8217;s milestones due to these constraints.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of weeks you would be able to work on the projects without violating the rules mentioned above<\/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> milestones = [1,2,3]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> One possible scenario is:\n\u200b\u200b\u200b\u200b- During the 1<sup>st<\/sup> week, you will work on a milestone of project 0.\n- During the 2<sup>nd<\/sup> week, you will work on a milestone of project 2.\n- During the 3<sup>rd<\/sup> week, you will work on a milestone of project 1.\n- During the 4<sup>th<\/sup> week, you will work on a milestone of project 2.\n- During the 5<sup>th<\/sup> week, you will work on a milestone of project 1.\n- During the 6<sup>th<\/sup> week, you will work on a milestone of project 2.\nThe total number of weeks is 6.\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> milestones = [5,2,1]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> One possible scenario is:\n- During the 1<sup>st<\/sup> week, you will work on a milestone of project 0.\n- During the 2<sup>nd<\/sup> week, you will work on a milestone of project 1.\n- During the 3<sup>rd<\/sup> week, you will work on a milestone of project 0.\n- During the 4<sup>th<\/sup> week, you will work on a milestone of project 1.\n- During the 5<sup>th<\/sup> week, you will work on a milestone of project 0.\n- During the 6<sup>th<\/sup> week, you will work on a milestone of project 2.\n- During the 7<sup>th<\/sup> week, you will work on a milestone of project 0.\nThe total number of weeks is 7.\nNote that you cannot work on the last milestone of project 0 on 8<sup>th<\/sup> week because it would violate the rules.\nThus, one milestone in project 0 will remain unfinished.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == milestones.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= milestones[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Let x be the longest project.<\/p>\n\n\n\n<p>Case 1: x > sum of the rest. <\/p>\n\n\n\n<p>Obviously, we cannot finish it.<br>The best we can do is : [x, a}, {x, b}, {x, c}, &#8230;., {x, z}, x.<br>Ans = 2 * rest + 1<\/p>\n\n\n\n<p>Case 2: <meta charset=\"utf-8\">x &lt;= sum of the rest. <\/p>\n\n\n\n<p>We can finish all the projects by alternating them properly.<br>Ans = sum<\/p>\n\n\n\n<p>Time complexity: O(n)<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  long long numberOfWeeks(vector<int>& milestones) {\n    long long sum = accumulate(begin(milestones), end(milestones), 0LL);\n    long long longest = *max_element(begin(milestones), end(milestones));\n    return min(sum, 2 * (sum - longest) + 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;projects numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are given an integer array&nbsp;milestones&nbsp;where each&nbsp;milestones[i]&nbsp;denotes the number of milestones the&nbsp;ith&nbsp;project has. You can work on the projects&#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":[88,31,177],"class_list":["post-9323","post","type-post","status-publish","format-standard","hentry","category-math","tag-greedy","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9323","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=9323"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9323\/revisions"}],"predecessor-version":[{"id":9326,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9323\/revisions\/9326"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}