{"id":527,"date":"2020-09-26T10:52:19","date_gmt":"2020-09-26T02:52:19","guid":{"rendered":"http:\/\/39.96.58.60\/?p=527"},"modified":"2022-10-18T16:38:55","modified_gmt":"2022-10-18T08:38:55","slug":"leetcode-1111-%e6%9c%89%e6%95%88%e6%8b%ac%e5%8f%b7%e7%9a%84%e5%b5%8c%e5%a5%97%e6%b7%b1%e5%ba%a6","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=527","title":{"rendered":"Leetcode 1111. \u6709\u6548\u62ec\u53f7\u7684\u5d4c\u5957\u6df1\u5ea6"},"content":{"rendered":"<p><strong>\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32 <\/strong>\u5b9a\u4e49\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u5de6\u62ec\u53f7\uff0c\u90fd\u80fd\u627e\u5230\u4e0e\u4e4b\u5bf9\u5e94\u7684\u53f3\u62ec\u53f7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8be6\u60c5\u53c2\u89c1\u9898\u672b\u300c<strong>\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32<\/strong>\u300d\u90e8\u5206\u3002<\/p>\n<p><strong>\u5d4c\u5957\u6df1\u5ea6<\/strong> <code>depth<\/code> \u5b9a\u4e49\uff1a\u5373\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\u5d4c\u5957\u7684\u5c42\u6570\uff0c<code>depth(A)<\/code> \u8868\u793a\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32 <code>A<\/code> \u7684\u5d4c\u5957\u6df1\u5ea6\u3002\u8be6\u60c5\u53c2\u89c1\u9898\u672b\u300c<strong>\u5d4c\u5957\u6df1\u5ea6<\/strong>\u300d\u90e8\u5206\u3002<\/p>\n<p>\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\u7c7b\u578b\u4e0e\u5bf9\u5e94\u7684\u5d4c\u5957\u6df1\u5ea6\u8ba1\u7b97\u65b9\u6cd5\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/04\/01\/1111.png\" style=\"height: 152px; width: 600px;\"><\/p>\n<p>&nbsp;<\/p>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u300c\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\u300d <code>seq<\/code>\uff0c\u8bf7\u4f60\u5c06\u5176\u5206\u6210\u4e24\u4e2a\u4e0d\u76f8\u4ea4\u7684\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\uff0c<code>A<\/code> \u548c&nbsp;<code>B<\/code>\uff0c\u5e76\u4f7f\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u6df1\u5ea6\u6700\u5c0f\u3002<\/p>\n<ul>\n<li>\u4e0d\u76f8\u4ea4\uff1a\u6bcf\u4e2a <code>seq[i]<\/code> \u53ea\u80fd\u5206\u7ed9 <code>A<\/code> \u548c <code>B<\/code> \u4e8c\u8005\u4e2d\u7684\u4e00\u4e2a\uff0c\u4e0d\u80fd\u65e2\u5c5e\u4e8e <code>A<\/code> \u4e5f\u5c5e\u4e8e <code>B<\/code> \u3002<\/li>\n<li><code>A<\/code> \u6216 <code>B<\/code> \u4e2d\u7684\u5143\u7d20\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u53ef\u4ee5\u4e0d\u8fde\u7eed\u3002<\/li>\n<li><code>A.length + B.length = seq.length<\/code><\/li>\n<li>\u6df1\u5ea6\u6700\u5c0f\uff1a<code>max(depth(A), depth(B))<\/code>&nbsp;\u7684\u53ef\u80fd\u53d6\u503c\u6700\u5c0f\u3002&nbsp;<\/li>\n<\/ul>\n<p>\u5212\u5206\u65b9\u6848\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a <code>seq.length<\/code> \u7684\u7b54\u6848\u6570\u7ec4 <code>answer<\/code> \u8868\u793a\uff0c\u7f16\u7801\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><code>answer[i] = 0<\/code>\uff0c<code>seq[i]<\/code> \u5206\u7ed9 <code>A<\/code> \u3002<\/li>\n<li><code>answer[i] = 1<\/code>\uff0c<code>seq[i]<\/code> \u5206\u7ed9 <code>B<\/code> \u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u5b58\u5728\u591a\u4e2a\u6ee1\u8db3\u8981\u6c42\u7684\u7b54\u6848\uff0c\u53ea\u9700\u8fd4\u56de\u5176\u4e2d\u4efb\u610f <strong>\u4e00\u4e2a <\/strong>\u5373\u53ef\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>seq = \"(()())\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[0,1,1,1,1,0]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>seq = \"()(())()\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[0,0,0,1,1,0,1,1]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u672c\u793a\u4f8b\u7b54\u6848\u4e0d\u552f\u4e00\u3002\n\n\u6309\u6b64\u8f93\u51fa A = \"()()\", B = \"()()\", max(depth(A), depth(B)) = 1\uff0c\u5b83\u4eec\u7684\u6df1\u5ea6\u6700\u5c0f\u3002\n\n\u50cf [1,1,1,0,0,1,1,1]\uff0c\u4e5f\u662f\u6b63\u786e\u7ed3\u679c\uff0c\u5176\u4e2d A = \"()()()\", B = \"()\", max(depth(A), depth(B)) = 1 \u3002 \n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;&nbsp;seq.size &lt;= 10000<\/code><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>\u4ec5\u7531&nbsp;<code>\"(\"<\/code> \u548c&nbsp;<code>\")\"<\/code>&nbsp;\u6784\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5de6\u62ec\u53f7\uff0c\u90fd\u80fd\u627e\u5230\u4e0e\u4e4b\u5bf9\u5e94\u7684\u53f3\u62ec\u53f7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\n\n\u4e0b\u8ff0\u51e0\u79cd\u60c5\u51b5\u540c\u6837\u5c5e\u4e8e\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\uff1a\n\n\n\n  1. \u7a7a\u5b57\u7b26\u4e32\n\n  2. \u8fde\u63a5\uff0c\u53ef\u4ee5\u8bb0\u4f5c&nbsp;<code>AB<\/code>\uff08<code>A<\/code> \u4e0e <code>B<\/code> \u8fde\u63a5\uff09\uff0c\u5176\u4e2d&nbsp;<code>A<\/code>&nbsp;\u548c&nbsp;<code>B<\/code>&nbsp;\u90fd\u662f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\n\n  3. \u5d4c\u5957\uff0c\u53ef\u4ee5\u8bb0\u4f5c&nbsp;<code>(A)<\/code>\uff0c\u5176\u4e2d&nbsp;<code>A<\/code>&nbsp;\u662f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\n\n<\/pre>\n<p><strong>\u5d4c\u5957\u6df1\u5ea6\uff1a<\/strong><\/p>\n<pre>\u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4efb\u610f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32 <code>s<\/code> \u7684 <strong>\u5d4c\u5957\u6df1\u5ea6<\/strong>&nbsp;<code>depth(S)<\/code>\uff1a\n\n\n\n  1.<code> s<\/code> \u4e3a\u7a7a\u65f6\uff0c<code>depth(\"\") = 0<\/code>\n\n<code>  2. s<\/code> \u4e3a <code>A<\/code> \u4e0e <code>B<\/code> \u8fde\u63a5\u65f6\uff0c<code>depth(A + B) = max(depth(A), depth(B))<\/code>\uff0c\u5176\u4e2d&nbsp;<code>A<\/code> \u548c&nbsp;<code>B<\/code>&nbsp;\u90fd\u662f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\n\n<code>  3. s<\/code> \u4e3a\u5d4c\u5957\u60c5\u51b5\uff0c<code>depth(\"(\" + A + \")\") = 1 + depth(A)<\/code>\uff0c\u5176\u4e2d <code>A<\/code> \u662f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\n\n\n\n\u4f8b\u5982\uff1a<code>\"\"<\/code>\uff0c<code>\"()()\"<\/code>\uff0c\u548c&nbsp;<code>\"()(()())\"<\/code>&nbsp;\u90fd\u662f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\uff0c\u5d4c\u5957\u6df1\u5ea6\u5206\u522b\u4e3a 0\uff0c1\uff0c2\uff0c\u800c&nbsp;<code>\")(\"<\/code> \u548c&nbsp;<code>\"(()\"<\/code>&nbsp;\u90fd\u4e0d\u662f\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\u3002\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u8d2a\u5fc3\u7b97\u6cd5\u3001 \u4e8c\u5206\u67e5\u627e\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\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.57% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8610.68% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5148\u7edf\u8ba1\u62ec\u53f7\u5d4c\u5957\u6df1\u5ea6\uff0c\u7136\u540e\u8fdb\u884c\u5206\u914d\n\"\"\"\nclass Solution:\n    def maxDepthAfterSplit(self, seq: str) -&gt; List[int]:\n        d, max_d = 0, 0 # \u5f53\u524d\u6df1\u5ea6\uff0c\u6700\u5927\u6df1\u5ea6\n        result = []\n        for s in seq:\n            max_d = max(d, max_d)\n            if s == \"(\":    # \u5de6\u62ec\u53f7\n                d += 1      # \u5148\u6df1\u5ea6+1\n                result.append(d)    # \u7136\u540e\u5c06\u5f53\u524d\u62ec\u53f7\u6df1\u5ea6\u6dfb\u52a0\u5230\u7ed3\u679c\u4e2d\n            if s == \")\":    # \u53f3\u62ec\u53f7\n                result.append(d)    # \u5148\u5c06\u5f53\u524d\u62ec\u53f7\u6df1\u5ea6\u6dfb\u52a0\u5230\u7ed3\u679c\u4e2d\n                d -= 1              # \u7136\u540e\u6df1\u5ea6-1\n        max_ = max_d\/\/2 # \u4ee5\u6df1\u5ea6\uff0c\u5206\u4e3a\u4e24\u90e8\u5206\n        result = [1 if r &gt; max_ else 0 for r in result ] # \u91cd\u65b0\u6574\u7406\u7ed3\u679c\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32 \u5b9a\u4e49\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u5de6\u62ec\u53f7\uff0c\u90fd\u80fd\u627e\u5230\u4e0e\u4e4b\u5bf9\u5e94\u7684\u53f3\u62ec\u53f7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u8be6\u60c5\u53c2\u89c1\u9898\u672b\u300c\u6709\u6548\u62ec\u53f7\u5b57\u7b26\u4e32\u300d\u90e8\u5206\u3002&#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\/527"}],"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=527"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":528,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/527\/revisions\/528"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}