{"id":369,"date":"2020-09-26T10:32:51","date_gmt":"2020-09-26T02:32:51","guid":{"rendered":"http:\/\/39.96.58.60\/?p=369"},"modified":"2022-10-18T16:39:35","modified_gmt":"2022-10-18T08:39:35","slug":"leetcode-11-%e7%9b%9b%e6%9c%80%e5%a4%9a%e6%b0%b4%e7%9a%84%e5%ae%b9%e5%99%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=369","title":{"rendered":"Leetcode 11. \u76db\u6700\u591a\u6c34\u7684\u5bb9\u5668"},"content":{"rendered":"<p>\u7ed9\u4f60 <em>n<\/em> \u4e2a\u975e\u8d1f\u6574\u6570 <em>a<\/em><sub>1<\/sub>\uff0c<em>a<\/em><sub>2\uff0c<\/sub>&#8230;\uff0c<em>a<\/em><sub>n\uff0c<\/sub>\u6bcf\u4e2a\u6570\u4ee3\u8868\u5750\u6807\u4e2d\u7684\u4e00\u4e2a\u70b9&nbsp;(<em>i<\/em>,&nbsp;<em>a<sub>i<\/sub><\/em>) \u3002\u5728\u5750\u6807\u5185\u753b <em>n<\/em> \u6761\u5782\u76f4\u7ebf\uff0c\u5782\u76f4\u7ebf <em>i<\/em>&nbsp;\u7684\u4e24\u4e2a\u7aef\u70b9\u5206\u522b\u4e3a&nbsp;(<em>i<\/em>,&nbsp;<em>a<sub>i<\/sub><\/em>) \u548c (<em>i<\/em>, 0)\u3002\u627e\u51fa\u5176\u4e2d\u7684\u4e24\u6761\u7ebf\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e0e&nbsp;<em>x<\/em>&nbsp;\u8f74\u5171\u540c\u6784\u6210\u7684\u5bb9\u5668\u53ef\u4ee5\u5bb9\u7eb3\u6700\u591a\u7684\u6c34\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong>\u4f60\u4e0d\u80fd\u503e\u659c\u5bb9\u5668\uff0c\u4e14&nbsp;<em>n<\/em>&nbsp;\u7684\u503c\u81f3\u5c11\u4e3a 2\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><img alt=\"\" src=\"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg\" style=\"height: 287px; width: 600px;\"><\/p>\n<p><small>\u56fe\u4e2d\u5782\u76f4\u7ebf\u4ee3\u8868\u8f93\u5165\u6570\u7ec4 [1,8,6,2,5,4,8,3,7]\u3002\u5728\u6b64\u60c5\u51b5\u4e0b\uff0c\u5bb9\u5668\u80fd\u591f\u5bb9\u7eb3\u6c34\uff08\u8868\u793a\u4e3a\u84dd\u8272\u90e8\u5206\uff09\u7684\u6700\u5927\u503c\u4e3a&nbsp;49\u3002<\/small><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[1,8,6,2,5,4,8,3,7]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>49<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\u3001 \u53cc\u6307\u9488\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\uff1a64 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8685.40% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a15.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8641.13% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u53cc\u6307\u9488\u6cd5\n\n                                |\n                        |       |           |\n                        |       |           |       |\n                        |       |           |       |\n                    ---------------------------------------\n\n    \u9ad8\u5ea6\u65b9\u9762\uff1a\u6c34\u9762\u7684\u9ad8\u5ea6\uff0c\u53d6\u51b3\u4e8e\u4e24\u4fa7\u6700\u4f4e\u7684\u4e00\u4fa7\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u8981\u786e\u5b9a\u6700\u4f4e\u7684\u4e00\u4fa7\uff0c\u4e2d\u95f4\u533a\u57df\u7684\u6c34\u4f4d\u9ad8\u5ea6\u5c31\u4e0d\u4f1a\u9ad8\u4e8e\u5f53\u524d\u9ad8\u5ea6\n    \u5bbd\u5ea6\u65b9\u9762\uff1a\u7531\u4e8e\u6211\u4eec\u662f\u4ece\u4fe9\u4fa7\u4f9d\u6b21\u5f80\u4e2d\u95f4\u632a\u52a8\uff0c\u6c34\u9762\u7684\u5bbd\u5ea6\u4e5f\u4e0d\u4f1a\u5927\u4e8e\u6211\u4eec\u7684\u5f53\u524d\u5bbd\u5ea6\u3002\n    \u6700\u7ec8\u7ed3\u8bba\uff1a\u5f53\u524d\u7684\u4e24\u5835\u5899\u95f4\u7684\u6700\u5927\u9762\u79ef\uff0c\u5df2\u7ecf\u88ab\u77ee\u5899\u9650\u5236\uff0c\u65e0\u9700\u8003\u8651\u3002\n        \u6211\u4eec\u53ea\u9700\u79fb\u52a8\u77ee\u5899\uff0c\u53bb\u5728\u5269\u4e0b\u7684\u8f83\u9ad8\u5899\u4f53\u4e4b\u95f4\u5bfb\u627e\u6700\u5927\u9762\u79ef\u5373\u53ef\n\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca.\n\"\"\"\nclass Solution:\n    def maxArea(self, height) -&gt; int:\n        p, q = 0, len(height)-1     # p, q \u53cc\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u5934\u3001\u5c3e\n        max_ = 0\n        while q-p &gt; 0:      # \u82e5\u53cc\u6307\u9488\u5df2\u904d\u5386\u5b8c\uff0c\u5219\u9000\u51fa\u5faa\u73af\n            h_p, h_q = height[p], height[q] # \u53cc\u6307\u9488\u6307\u5411\u7684\u5899\u4f53\u9ad8\u5ea6\n            if h_p &lt; h_q:       # \u5411\u4e2d\u95f4\u79fb\u52a8\u77ee\u5899\u6307\u9488\uff0c\u8bb0\u5f55\u5e76\u66f4\u65b0\u6700\u5927\u9762\u79ef\n                area = h_p * (q - p)\n                if area &gt; max_:\n                    max_ = area\n                p += 1\n            else:\n                area = h_q * (q - p)\n                if area &gt; max_:\n                    max_ = area\n                q -= 1\n        return max_\n\n\n\"\"\"\n\u66b4\u529b\u7834\u89e3\uff0c\u5728leetcode\u4e0a\u662f\u884c\u4e0d\u901a\u7684\uff0c\u8d85\u65f6\u4e86\n\"\"\"\nclass Solution:\n    def maxArea(self, height) -&gt; int:\n        nums = len(height)\n        max_ = 0\n        for i in range(nums):\n            for j in range(i+1, nums):\n                area = (j-i) * min(height[i], height[j])\n                if area&gt; max_:\n                    max_ = area\n        return max_\n\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60 n \u4e2a\u975e\u8d1f\u6574\u6570 a1\uff0ca2\uff0c&#8230;\uff0can\uff0c\u6bcf\u4e2a\u6570\u4ee3\u8868\u5750\u6807\u4e2d\u7684\u4e00\u4e2a\u70b9&nbsp;(i,&nbsp;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/369"}],"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=369"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":370,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions\/370"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}