{"id":437,"date":"2020-09-26T10:41:30","date_gmt":"2020-09-26T02:41:30","guid":{"rendered":"http:\/\/39.96.58.60\/?p=437"},"modified":"2022-10-18T16:39:23","modified_gmt":"2022-10-18T08:39:23","slug":"leetcode-198-%e6%89%93%e5%ae%b6%e5%8a%ab%e8%88%8d","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=437","title":{"rendered":"Leetcode 198. \u6253\u5bb6\u52ab\u820d"},"content":{"rendered":"<p>\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u5c0f\u5077\uff0c\u8ba1\u5212\u5077\u7a83\u6cbf\u8857\u7684\u623f\u5c4b\u3002\u6bcf\u95f4\u623f\u5185\u90fd\u85cf\u6709\u4e00\u5b9a\u7684\u73b0\u91d1\uff0c\u5f71\u54cd\u4f60\u5077\u7a83\u7684\u552f\u4e00\u5236\u7ea6\u56e0\u7d20\u5c31\u662f\u76f8\u90bb\u7684\u623f\u5c4b\u88c5\u6709\u76f8\u4e92\u8fde\u901a\u7684\u9632\u76d7\u7cfb\u7edf\uff0c<strong>\u5982\u679c\u4e24\u95f4\u76f8\u90bb\u7684\u623f\u5c4b\u5728\u540c\u4e00\u665a\u4e0a\u88ab\u5c0f\u5077\u95ef\u5165\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u62a5\u8b66<\/strong>\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4ee3\u8868\u6bcf\u4e2a\u623f\u5c4b\u5b58\u653e\u91d1\u989d\u7684\u975e\u8d1f\u6574\u6570\u6570\u7ec4\uff0c\u8ba1\u7b97\u4f60<strong> \u4e0d\u89e6\u52a8\u8b66\u62a5\u88c5\u7f6e\u7684\u60c5\u51b5\u4e0b <\/strong>\uff0c\u4e00\u591c\u4e4b\u5185\u80fd\u591f\u5077\u7a83\u5230\u7684\u6700\u9ad8\u91d1\u989d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[1,2,3,1]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5077\u7a83 1 \u53f7\u623f\u5c4b (\u91d1\u989d = 1) \uff0c\u7136\u540e\u5077\u7a83 3 \u53f7\u623f\u5c4b (\u91d1\u989d = 3)\u3002\n\n&nbsp;    \u5077\u7a83\u5230\u7684\u6700\u9ad8\u91d1\u989d = 1 + 3 = 4 \u3002<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[2,7,9,3,1]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>12\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5077\u7a83 1 \u53f7\u623f\u5c4b (\u91d1\u989d = 2), \u5077\u7a83 3 \u53f7\u623f\u5c4b (\u91d1\u989d = 9)\uff0c\u63a5\u7740\u5077\u7a83 5 \u53f7\u623f\u5c4b (\u91d1\u989d = 1)\u3002\n\n&nbsp;    \u5077\u7a83\u5230\u7684\u6700\u9ad8\u91d1\u989d = 2 + 9 + 1 = 12 \u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>0 &lt;= nums.length &lt;= 100<\/code><\/li>\n<li><code>0 &lt;= nums[i] &lt;= 400<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \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\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8687.93% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8655.99% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n    \u7531\u4e8e\u4e0d\u80fd\u6328\u7740\u5077\u53d6\uff0c\u6240\u4ee5 \u5f53\u524d\u5077\u53d6\u7684\u503c\u4f9d\u8d56\u4e8e\u4e0a\u4e0a\u4e00\u4e2a\n        dp[i] == dp[i-2]+nums[i]\n    \u5f53\u524d\u5077\u53d6\u7684\u6700\u5927\u503c\u4e3a\n        dp[i] == max(dp[i-2]+nums[i], dp[i-1])\n\"\"\"\nclass Solution:\n    def rob(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        if n == 0:\n            return 0\n        if n &lt; 3:\n            return max(nums)\n        dp = [[] for _ in range(n)]\n        dp[0] = nums[0]\n        dp[1] = max(nums[:2])\n        for i in range(2, n):\n            dp[i] = max(dp[i-2] + nums[i], dp[i-1])\n        return max(dp)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u5c0f\u5077\uff0c\u8ba1\u5212\u5077\u7a83\u6cbf\u8857\u7684\u623f\u5c4b\u3002\u6bcf\u95f4\u623f\u5185\u90fd\u85cf\u6709\u4e00\u5b9a\u7684\u73b0\u91d1\uff0c\u5f71\u54cd\u4f60\u5077\u7a83\u7684\u552f\u4e00\u5236\u7ea6\u56e0\u7d20\u5c31\u662f\u76f8\u90bb\u7684\u623f\u5c4b\u88c5\u6709\u76f8&#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\/437"}],"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=437"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":438,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/437\/revisions\/438"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}