{"id":66,"date":"2022-03-10T15:00:00","date_gmt":"2022-03-10T07:00:00","guid":{"rendered":"https:\/\/blog.49cc.top\/?p=66"},"modified":"2022-03-19T15:01:13","modified_gmt":"2022-03-19T07:01:13","slug":"%e6%89%93%e5%ae%b6%e5%8a%ab%e8%88%8d","status":"publish","type":"post","link":"https:\/\/blog.rrr.ink\/?p=66","title":{"rendered":"\u6253\u5bb6\u52ab\u820d"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>\u9898\u76ee\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<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\n\n\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>\u00a0\u4e0d\u89e6\u52a8\u8b66\u62a5\u88c5\u7f6e\u7684\u60c5\u51b5\u4e0b\u00a0<\/strong>\uff0c\u4e00\u591c\u4e4b\u5185\u80fd\u591f\u5077\u7a83\u5230\u7684\u6700\u9ad8\u91d1\u989d\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u8f93\u5165\uff1a[1,2,3,1]\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u5077\u7a83 1 \u53f7\u623f\u5c4b (\u91d1\u989d = 1) \uff0c\u7136\u540e\u5077\u7a83 3 \u53f7\u623f\u5c4b (\u91d1\u989d = 3)\u3002\n\u00a0    \u5077\u7a83\u5230\u7684\u6700\u9ad8\u91d1\u989d = 1 + 3 = 4 \u3002\n\n\u8f93\u5165\uff1a[2,7,9,3,1]\r\n\u8f93\u51fa\uff1a12\r\n\u89e3\u91ca\uff1a\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\r\n     \u5077\u7a83\u5230\u7684\u6700\u9ad8\u91d1\u989d = 2 + 9 + 1 = 12 \u3002<\/pre>\n\n\n\n<p>\u63d0\u793a\uff1a<\/p>\n\n\n\n<ul><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 400<\/code><\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u601d\u8def\uff1a<\/strong><\/p>\n\n\n\n<p> \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u623f\u5c4b\uff0c\u5c31\u53ea\u80fd\u9009\u62e9\u8be5\u623f\u5c4b\uff0c\u679c\u7136\u4e24\u95f4\u9009\u62e9\u91d1\u989d\u8f83\u5927\u7684\u8fdb\u884c\u5077\u7a83\u3002\u5bf9\u4e8e\u8981\u5077\u7a83\u7684\u623f\u95f4  <code>k(k>2)<\/code> \u53ea\u6709\u4e24\u79cd\u9009\u62e9\uff1a<\/p>\n\n\n\n<p>1.\u9009\u62e9\u5077\u7a83  \u9009\u62e9\u5077\u7a83\u8be5\u623f\u95f4 \u90a3\u4e48\u5077\u7a83\u7684\u91d1\u989d\u4e3a\u8be5\u623f\u95f4\u91d1\u989d\u4e0e\u524d<code>k - 2<\/code> (\u4e0d\u76f8\u90bb) \u623f\u5c4b\u6700\u9ad8\u603b\u91d1\u989d\u7684\u548c\u3002 <\/p>\n\n\n\n<p>2. \u4e0d\u5077\u7a83\u8be5\u623f\u95f4\uff0c\u5077\u7a83\u603b\u91d1\u989d\u4e3a\u524d <code>k - 1<\/code>\u00a0\u95f4\u623f\u5c4b\u7684\u6700\u9ad8\u603b\u91d1\u989d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u90a3\u4e48\u53ef\u5f97\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a f(n) \uff08\u6700\u9ad8\u603b\u91d1\u989d\uff09 = Math.max(f(n - 2) + nums[n], nums[n - 1])<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u5b9e\u73b0\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package main\r\n\r\nimport (\r\n\t\"fmt\"\r\n)\r\n\r\nfunc main() {\r\n\tnums := []int{}\r\n\tfmt.Println(rob(nums)) \/\/12\r\n}\r\n\r\nfunc rob(nums []int) int {\r\n\tn := len(nums)\r\n\tif n == 0 {\r\n\t\treturn 0\r\n\t}\r\n\ta, b := 0, nums[0]\r\n\tfor i := 1; i &lt; n; i++ {\r\n\t\ta, b = b, max(nums[i]+a, b)\r\n\t}\r\n\treturn b\r\n\r\n}\r\n\r\nfunc max(a, b int) int {\r\n\tif a > b {\r\n\t\treturn a\r\n\t}\r\n\treturn b\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0\uff1a \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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=\/wp\/v2\/posts\/66"}],"collection":[{"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":4,"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rrr.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}