{"id":109482,"date":"2020-05-29T10:25:51","date_gmt":"2020-05-29T02:25:51","guid":{"rendered":"http:\/\/4563.org\/?p=109482"},"modified":"2020-05-29T10:25:51","modified_gmt":"2020-05-29T02:25:51","slug":"leetcode-lintcode-%e9%a2%98%e8%a7%a3-%e6%89%93%e5%8a%ab%e6%88%bf%e5%b1%8b-%c2%b7-house-robber","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=109482","title":{"rendered":"[leetcode\/lintcode \u9898\u89e3] \u6253\u52ab\u623f\u5c4b \u00b7 House Robber"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [leetcode\/lintcode \u9898\u89e3] \u6253\u52ab\u623f\u5c4b \u00b7 House Robber               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>[\u9898\u76ee\u63cf\u8ff0]<\/h2>\n<p>\u5047\u8bbe\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u7a83\u8d3c\uff0c\u51c6\u5907\u6cbf\u7740\u4e00\u6761\u8857\u6253\u52ab\u623f\u5c4b\u3002\u6bcf\u4e2a\u623f\u5b50\u90fd\u5b58\u653e\u7740\u7279\u5b9a\u91d1\u989d\u7684\u94b1\u3002\u4f60\u9762\u4e34\u7684\u552f\u4e00\u7ea6\u675f\u6761\u4ef6\u662f\uff1a\u76f8\u90bb\u7684\u623f\u5b50\u88c5\u7740\u76f8\u4e92\u8054\u7cfb\u7684\u9632\u76d7\u7cfb\u7edf\uff0c\u4e14\u00a0<strong>\u5f53\u76f8\u90bb\u7684\u4e24\u4e2a\u623f\u5b50\u540c\u4e00\u5929\u88ab\u6253\u52ab\u65f6\uff0c\u8be5\u7cfb\u7edf\u4f1a\u81ea\u52a8\u62a5\u8b66<\/strong>\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u5217\u8868\uff0c\u8868\u793a\u6bcf\u4e2a\u623f\u5b50\u4e2d\u5b58\u653e\u7684\u94b1\uff0c \u7b97\u4e00\u7b97\uff0c\u5982\u679c\u4eca\u665a\u53bb\u6253\u52ab\uff0c<strong>\u5728\u4e0d\u89e6\u52a8\u62a5\u8b66\u88c5\u7f6e\u7684\u60c5\u51b5\u4e0b<\/strong>, \u4f60\u6700\u591a\u53ef\u4ee5\u5f97\u5230\u591a\u5c11\u94b1 \u3002<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740:<\/p>\n<p>https:\/\/www.lintcode.com\/problem\/house-robber\/?utm_source=sc-v2ex-fks0529<\/p>\n<h2>[\u6837\u4f8b]<\/h2>\n<p>\u6837\u4f8b 1:<\/p>\n<pre><code>\u8f93\u5165: [3, 8, 4] \u8f93\u51fa: 8 \u89e3\u91ca: \u4ec5\u4ec5\u6253\u52ab\u7b2c\u4e8c\u4e2a\u623f\u5b50 <\/code><\/pre>\n<p>\u6837\u4f8b 2:<\/p>\n<pre><code>\u8f93\u5165: [5, 2, 1, 3]  \u8f93\u51fa: 8 \u89e3\u91ca: \u62a2\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u623f\u5b50 <\/code><\/pre>\n<h2>[\u9898\u89e3]<\/h2>\n<p>\u7ebf\u6027 DP \u9898\u76ee.<\/p>\n<p>\u8bbe dp[i] \u8868\u793a\u524d i \u5bb6\u623f\u5b50\u6700\u591a\u6536\u76ca, \u7b54\u6848\u662f dp[n], \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u662f<\/p>\n<p>dp[i] = max(dp[i-1], dp[i-2] + A[i-1])<\/p>\n<p>\u8003\u8651\u5230 dp[i]\u7684\u8ba1\u7b97\u53ea\u6d89\u53ca\u5230 dp[i-1]\u548c dp[i-2], \u56e0\u6b64\u53ef\u4ee5 O(1)\u7a7a\u95f4\u89e3\u51b3.<\/p>\n<pre><code>public class Solution {     \/**      * @param A: An array of non-negative integers.      * return: The maximum amount of money you can rob tonight      *\/     public long houseRobber(int[] A) {         int n = A.length;         if (n == 0)             return 0;         long []res = new long[n+1];          res[0] = 0;         res[1] = A[0];         for (int i = 2; i &lt;= n; i++) {             res[i] = Math.max(res[i-1], res[i-2] + A[i-1]);         }         return res[n];     } }  \/\/\/\/\/\/\/\/\/\/\/\/ \u7a7a\u95f4\u590d\u6742\u5ea6 O(1)\u7248\u672c  public class Solution {     \/**      * @param A: An array of non-negative integers.      * return: The maximum amount of money you can rob tonight      *\/     public long houseRobber(int[] A) {         int n = A.length;         if (n == 0)             return 0;         long []res = new long[2];          res[0] = 0;         res[1] = A[0];         for (int i = 2; i &lt;= n; i++) {             res[i%3] = Math.max(res[(i-1)%2], res[(i-2)%2] + A[i-1]);         }         return res[n%2];     }     <\/code><\/pre>\n<h2>[\u66f4\u591a\u9898\u89e3\u53c2\u8003]<\/h2>\n<p>https:\/\/www.jiuzhang.com\/solution\/house-robber\/?utm_source=sc-v2ex-fks0529<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[leetcode\/lintcod&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/109482"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109482"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/109482\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109482"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}