{"id":780,"date":"2020-12-07T13:55:29","date_gmt":"2020-12-07T05:55:29","guid":{"rendered":"http:\/\/39.96.58.60\/?p=780"},"modified":"2022-10-18T16:37:12","modified_gmt":"2022-10-18T08:37:12","slug":"leetcode-621-%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%e5%99%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=780","title":{"rendered":"Leetcode 621. \u4efb\u52a1\u8c03\u5ea6\u5668"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u7528\u5b57\u7b26\u6570\u7ec4&nbsp;<code>tasks<\/code> \u8868\u793a\u7684 CPU \u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\u5217\u8868\u3002\u5176\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u8868\u793a\u4e00\u79cd\u4e0d\u540c\u79cd\u7c7b\u7684\u4efb\u52a1\u3002\u4efb\u52a1\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6267\u884c\uff0c\u5e76\u4e14\u6bcf\u4e2a\u4efb\u52a1\u90fd\u53ef\u4ee5\u5728 1 \u4e2a\u5355\u4f4d\u65f6\u95f4\u5185\u6267\u884c\u5b8c\u3002\u5728\u4efb\u4f55\u4e00\u4e2a\u5355\u4f4d\u65f6\u95f4\uff0cCPU \u53ef\u4ee5\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\uff0c\u6216\u8005\u5904\u4e8e\u5f85\u547d\u72b6\u6001\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4e24\u4e2a<strong> \u76f8\u540c\u79cd\u7c7b<\/strong> \u7684\u4efb\u52a1\u4e4b\u95f4\u5fc5\u987b\u6709\u957f\u5ea6\u4e3a\u6574\u6570<strong> <\/strong><code>n<\/code><strong> <\/strong>\u7684\u51b7\u5374\u65f6\u95f4\uff0c\u56e0\u6b64\u81f3\u5c11\u6709\u8fde\u7eed <code>n<\/code> \u4e2a\u5355\u4f4d\u65f6\u95f4\u5185 CPU \u5728\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\uff0c\u6216\u8005\u5728\u5f85\u547d\u72b6\u6001\u3002<\/p>\n<p>\u4f60\u9700\u8981\u8ba1\u7b97\u5b8c\u6210\u6240\u6709\u4efb\u52a1\u6240\u9700\u8981\u7684<strong> \u6700\u77ed\u65f6\u95f4<\/strong> \u3002<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>tasks = [\"A\",\"A\",\"A\",\"B\",\"B\",\"B\"], n = 2\n\n<strong>\u8f93\u51fa\uff1a<\/strong>8\n\n<strong>\u89e3\u91ca\uff1a<\/strong>A -&gt; B -&gt; (\u5f85\u547d) -&gt; A -&gt; B -&gt; (\u5f85\u547d) -&gt; A -&gt; B\n\n     \u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u4e24\u4e2a\u76f8\u540c\u7c7b\u578b\u4efb\u52a1\u4e4b\u95f4\u5fc5\u987b\u95f4\u9694\u957f\u5ea6\u4e3a n = 2 \u7684\u51b7\u5374\u65f6\u95f4\uff0c\u800c\u6267\u884c\u4e00\u4e2a\u4efb\u52a1\u53ea\u9700\u8981\u4e00\u4e2a\u5355\u4f4d\u65f6\u95f4\uff0c\u6240\u4ee5\u4e2d\u95f4\u51fa\u73b0\u4e86\uff08\u5f85\u547d\uff09\u72b6\u6001\u3002<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>tasks = [\"A\",\"A\",\"A\",\"B\",\"B\",\"B\"], n = 0\n\n<strong>\u8f93\u51fa\uff1a<\/strong>6\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4efb\u4f55\u5927\u5c0f\u4e3a 6 \u7684\u6392\u5217\u90fd\u53ef\u4ee5\u6ee1\u8db3\u8981\u6c42\uff0c\u56e0\u4e3a n = 0\n\n[\"A\",\"A\",\"A\",\"B\",\"B\",\"B\"]\n\n[\"A\",\"B\",\"A\",\"B\",\"A\",\"B\"]\n\n[\"B\",\"B\",\"B\",\"A\",\"A\",\"A\"]\n\n...\n\n\u8bf8\u5982\u6b64\u7c7b\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>tasks = [\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\"], n = 2\n\n<strong>\u8f93\u51fa\uff1a<\/strong>16\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e00\u79cd\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u662f\uff1a\n\n     A -&gt; B -&gt; C -&gt; A -&gt; D -&gt; E -&gt; A -&gt; F -&gt; G -&gt; A -&gt; (\u5f85\u547d) -&gt; (\u5f85\u547d) -&gt; A -&gt; (\u5f85\u547d) -&gt; (\u5f85\u547d) -&gt; A\n\n<\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= task.length &lt;= 10<sup>4<\/sup><\/code><\/li>\n<li><code>tasks[i]<\/code> \u662f\u5927\u5199\u82f1\u6587\u5b57\u6bcd<\/li>\n<li><code>n<\/code> \u7684\u53d6\u503c\u8303\u56f4\u4e3a <code>[0, 100]<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u8d2a\u5fc3\u7b97\u6cd5\u3001 \u961f\u5217\u3001 \u6570\u7ec4\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a44 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e86100.00% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8630.58% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\u8d2a\u5fc3\u7b97\u6cd5\n    tasks = [\"A\",\"A\",\"A\",\"B\",\"B\",\"B\",\"C\",\"C\",\"C\",\"D\",\"E\"], n = 2, leastInterval = 16\n\n        A   B   C   D\n        A   B   C   E\n        A   B   C\n\n    tasks = [\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\"], n = 2, leastInterval = 16\n\n        A   B   C\n        A   D   E\n        A   F   G\n        A   _   _\n        A   _   _\n        A\n\"\"\"\nclass Solution:\n    def leastInterval(self, tasks: List[str], n: int) -&gt; int:\n        count = collections.Counter(tasks)  # \u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\n        h = max([v for _,v in count.items()])   # \u9009\u6700\u5927\u7684\u4f5c\u4e3a\u77e9\u5f62\u9ad8\n        w = len([v for _,v in count.items() if v == h]) # \u5c06\u51fa\u73b0\u6b21\u6570\u7b49\u4e8eh\u7684\u5143\u7d20\u5148\u653e\u8fdb\u77e9\u9635\uff0c\u6b64\u65f6\uff0c\u77e9\u9635\u5bbdw\n        ew = max(math.ceil((len(tasks) - h*w)\/h), n-w+1)    # \u5269\u4f59\u5b57\u7b26\u653e\u5165\u77e9\u9635\uff0c\u6240\u9700\u7684\u989d\u5916\u5bbdew\n\n        if n - w + 1 &gt; (len(tasks) - h * w) \/ h:    # \u82e5\u5269\u4f59\u5b57\u7b26\u672a\u653e\u6ee1ew*h\n            num = h * w + max((len(tasks) - h * w), ew * (h - 1))\n        else:                                       # \u82e5\u5269\u4f59\u5b57\u7b26\u8d85\u51fa\u4e86ew*h, h * w + len(tasks) - h * w\n            num = len(tasks)\n        return num\n\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u7528\u5b57\u7b26\u6570\u7ec4&nbsp;tasks \u8868\u793a\u7684 CPU \u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\u5217\u8868\u3002\u5176\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u8868\u793a\u4e00\u79cd\u4e0d\u540c\u79cd\u7c7b\u7684\u4efb&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/780"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=780"}],"version-history":[{"count":2,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions"}],"predecessor-version":[{"id":782,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions\/782"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}