{"id":503,"date":"2020-09-26T10:49:26","date_gmt":"2020-09-26T02:49:26","guid":{"rendered":"http:\/\/39.96.58.60\/?p=503"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-300-%e6%9c%80%e9%95%bf%e4%b8%8a%e5%8d%87%e5%ad%90%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=503","title":{"rendered":"Leetcode 300. \u6700\u957f\u4e0a\u5347\u5b50\u5e8f\u5217"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5e8f\u7684\u6574\u6570\u6570\u7ec4\uff0c\u627e\u5230\u5176\u4e2d\u6700\u957f\u4e0a\u5347\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> <code>[10,9,2,5,3,7,101,18]\n\n<\/code><strong>\u8f93\u51fa: <\/strong>4 \n\n<strong>\u89e3\u91ca: <\/strong>\u6700\u957f\u7684\u4e0a\u5347\u5b50\u5e8f\u5217\u662f&nbsp;<code>[2,3,7,101]\uff0c<\/code>\u5b83\u7684\u957f\u5ea6\u662f <code>4<\/code>\u3002<\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ul>\n<li>\u53ef\u80fd\u4f1a\u6709\u591a\u79cd\u6700\u957f\u4e0a\u5347\u5b50\u5e8f\u5217\u7684\u7ec4\u5408\uff0c\u4f60\u53ea\u9700\u8981\u8f93\u51fa\u5bf9\u5e94\u7684\u957f\u5ea6\u5373\u53ef\u3002<\/li>\n<li>\u4f60\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5e94\u8be5\u4e3a&nbsp;O(<em>n<sup>2<\/sup><\/em>) \u3002<\/li>\n<\/ul>\n<p><strong>\u8fdb\u9636:<\/strong> \u4f60\u80fd\u5c06\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230&nbsp;O(<em>n<\/em> log <em>n<\/em>) \u5417?<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u4e8c\u5206\u67e5\u627e\u3001 \u52a8\u6001\u89c4\u5212\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\uff1a1052 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8663.37% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8615.97% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    i'm stupid!\n    \u53c2\u7167\u7684\u5b98\u7f51\u7ed9\u7684\u601d\u8def\u5199\u7684\n    \u5f53\u904d\u5386\u5230\u4e00\u4e2a\u5b57\u7b26\u65f6\uff0c\u5fc5\u987b\u6328\u7740\u53bb\u4e0e\u524d\u9762\u7684\u8fdb\u884c\u6bd4\u8f83\uff0c\u7136\u540e\u5f97\u51fa\u8be5\u5b57\u7b26\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u6700\u5927\u957f\u5ea6\n\n    \u8fd8\u6709\u5176\u4ed6\u66f4\u5feb\u7684\u89e3\u6cd5\uff0c\u4f46\u7531\u4e8e\u5f53\u524d\u4e3b\u8981\u5728\u7ec3\u4e60\u52a8\u6001\u89c4\u5212\n\"\"\"\nclass Solution:\n    def lengthOfLIS(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        if n == 0:\n            return 0\n        dp = [[] for _ in range(n)]\n        dp[0] = 1\n        for i in range(1, n):\n            max_ = -1\n            for j in range(i):\n                if nums[i] &gt; nums[j]:\n                    max_ = max(max_, dp[j])\n            if max_ == -1:\n                dp[i] = 1\n            else:\n                dp[i] = max_ + 1\n        return max(dp)\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.67% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8670.80% \u7684\u7528\u6237\n\u89e3\u9898\u601d\u8def\uff1a\n    \u8d2a\u5fc3\u7b97\u6cd5\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n    \u4e3a\u4e86\u4f7f\u4e0a\u5347\u5b50\u5e8f\u5217\u5c3d\u53ef\u80fd\u7684\u957f\uff0c \n    \u7b49\u540c\u4e8e\u4f7f \u4e0a\u5347\u5b50\u5e8f\u5217\u4e0a\u5347\u901f\u5ea6\u5c3d\u53ef\u80fd\u6162,\n    \u7b49\u540c\u4e8e\uff0c\u6bcf\u6b21\u6dfb\u52a0\u8fdb\u5b50\u5e8f\u5217\u7684\u503c\uff0c\u8981\u5c3d\u53ef\u80fd\u5c0f\n    \n    \u4f8b\uff1a  [4,10,4,3,8,9]\n        num     records\n        4       4\n        10      4,10\n        4       4,10\n        3       3,10\n        8       3,8\n        9       3,8,9\n                size = 3\n    \n    \u4f8b\uff1a  [10,9,2,5,3,7,101,18]\n    \n        num     records\n        10      10\n        9       9\n        2       2\n        5       2,5\n        3       2,3\n        7       2,3,7\n        101     2,3,7,101\n        8       2,3,7,18\n                size = 4\n    \n\"\"\"\nclass Solution:\n    def lengthOfLIS(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        if n &lt; 2:       # \u5982\u679c \u957f\u5ea6\u4e3a0 \u6216\u4e3a1\uff0c \u76f4\u63a5\u8fd4\u56de 0 or 1\n            return n\n        size = 1            # \u521d\u59cb\u957f\u5ea6\u4e3a1\n        records = [nums[0]] # \u8bb0\u5f55\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n        for num in nums[1:]:    # \u4ece\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5f00\u59cb\u6bd4\u8f83\n            if num &gt; records[-1]:   # \u5982\u679c\uff0c\u5f53\u524d\u6570\u5b57\uff0c\u6bd4\u8bb0\u5f55\u7684\u6700\u5927\u503c\u5927\uff0c\u5219\u8fdb\u5217\u8868\uff0c \u957f\u5ea6+1\n                records.append(num)\n                size += 1\n            else:\n                for i, record in enumerate(records):    # \u5426\u5219\uff0c\u6bd4\u8f83\u8bb0\u5f55\u7684\u5143\u7d20\uff0c\u4f7f\u7528\u6570\u5b57\u66ff\u6362\u5927\u4e8e\u8be5\u6570\u5b57\u7684\u7b2c\u4e00\u4e2a\u8bb0\u5f55\n                    if num &lt;= record:\n                        records[i] = num\n                        break\n        return size<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5e8f\u7684\u6574\u6570\u6570\u7ec4\uff0c\u627e\u5230\u5176\u4e2d\u6700\u957f\u4e0a\u5347\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u3002 \u793a\u4f8b: \u8f93\u5165: [10,9,2,5,3,7,101,1&#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\/503"}],"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=503"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":504,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions\/504"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}