{"id":505,"date":"2020-09-26T10:49:39","date_gmt":"2020-09-26T02:49:39","guid":{"rendered":"http:\/\/39.96.58.60\/?p=505"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-845-%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e6%9c%80%e9%95%bf%e5%b1%b1%e8%84%89","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=505","title":{"rendered":"Leetcode 845. \u6570\u7ec4\u4e2d\u7684\u6700\u957f\u5c71\u8109"},"content":{"rendered":"<p>\u6211\u4eec\u628a\u6570\u7ec4 A \u4e2d\u7b26\u5408\u4e0b\u5217\u5c5e\u6027\u7684\u4efb\u610f\u8fde\u7eed\u5b50\u6570\u7ec4 B \u79f0\u4e3a \u201c<em>\u5c71\u8109\u201d<\/em>\uff1a<\/p>\n<ul>\n<li><code>B.length &gt;= 3<\/code><\/li>\n<li>\u5b58\u5728 <code>0 &lt; i&nbsp;&lt; B.length - 1<\/code> \u4f7f\u5f97 <code>B[0] &lt; B[1] &lt; ... B[i-1] &lt; B[i] &gt; B[i+1] &gt; ... &gt; B[B.length - 1]<\/code><\/li>\n<\/ul>\n<p>\uff08\u6ce8\u610f\uff1aB \u53ef\u4ee5\u662f A \u7684\u4efb\u610f\u5b50\u6570\u7ec4\uff0c\u5305\u62ec\u6574\u4e2a\u6570\u7ec4 A\u3002\uff09<\/p>\n<p>\u7ed9\u51fa\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>A<\/code>\uff0c\u8fd4\u56de\u6700\u957f <em>\u201c\u5c71\u8109\u201d<\/em>&nbsp;\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u542b\u6709 \u201c<em>\u5c71\u8109\u201d&nbsp;<\/em>\u5219\u8fd4\u56de <code>0<\/code>\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[2,1,4,7,3,2,5]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>5\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u957f\u7684 \u201c\u5c71\u8109\u201d \u662f [1,4,7,3,2]\uff0c\u957f\u5ea6\u4e3a 5\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[2,2,2]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e0d\u542b \u201c\u5c71\u8109\u201d\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= A.length &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 10000<\/code><\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \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\u4e8697.12% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.1 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8678.97% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6a21\u62df\u63d0\u8bae\uff0c\u8bb0\u5f55\u5f53\u524d\u8d77\u4f0f\u72b6\u6001\uff0c\u5206\u6790\u662f\u5426\u662f\u5c71\u8109\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def longestMountain(self, A: List[int]) -&gt; int:\n        s, p, l = 0, 0, 0   # \u8d77\u59cb\u70b9\uff0cp\u6307\u9488\uff0c\u8bb0\u5f55\u5f53\u524d\u6700\u957f\u5c71\u8109\u957f\u5ea6\n        up, down = False, False # \u8bb0\u5f55\u5f53\u524d\u8d77\u4f0f\u72b6\u6001\n        while p &lt; len(A)-1:\n            if A[p] &gt; A[p+1]: # \u5f53\u524d\u4e0b\u964d\n                if up:  # \u5982\u679c\u4e4b\u524d\u5b58\u5728\u4e0a\u5347\u72b6\u6001\uff0c\u5219\u5b58\u5728\u5c71\u8109\uff0c\u6fc0\u6d3b\u5c71\u8109\u7684\u4e0b\u964d\u72b6\u6001\n                    down = True\n                else:\n                    s = p+1 # \u5426\u5219\uff0c\u4e4b\u524d\u4e0d\u5b58\u5728\u5c71\u8109\uff0c\u5c06\u632a\u52a8\u8d77\u59cb\u70b9\n            elif A[p] &lt; A[p+1]:   # \u5f53\u524d\u4e0a\u5347\n                if up == True and down == True: # \u5982\u679c\u4e0a\u5347\u548c\u4e0b\u964d\u72b6\u6001\u6fc0\u6d3b\uff0c \u5219\u5b58\u5728\u5c71\u8109\uff0c\u4e14\u5c71\u8109\u7ed3\u675f\n                    l = max(l, p+1-s)   # \u66f4\u65b0\u5c71\u8109\u957f\u5ea6\n                    s = p   # \u632a\u52a8\u8d77\u59cb\u70b9\n                    down = False    # \u91cd\u7f6e\u8d77\u4f0f\u72b6\u6001\n                up = True\n            else:   # \u5e73\u5730\n                if up == True and down == True: # \u5982\u679c\u4e4b\u524d\u5b58\u5728\u5c71\u8109\uff0c\u5219\u66f4\u65b0l\n                    l = max(l, p+1 - s)\n                up = False  # \u91cd\u7f6e\u8d77\u4f0f\u72b6\u6001\n                down = False\n                s = p+1 # \u632a\u52a8\u8d77\u59cb\u70b9\n            p += 1\n        if up == True and down == True: # \u5982\u679c\u5b58\u5728\u5c71\u8109\uff0c\u5219\u66f4\u65b0l\n            l = max(l, p+1 - s )\n        return l<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u628a\u6570\u7ec4 A \u4e2d\u7b26\u5408\u4e0b\u5217\u5c5e\u6027\u7684\u4efb\u610f\u8fde\u7eed\u5b50\u6570\u7ec4 B \u79f0\u4e3a \u201c\u5c71\u8109\u201d\uff1a B.length &gt;= 3 \u5b58\u5728 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","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\/505"}],"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=505"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":506,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions\/506"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}