{"id":1112,"date":"2017-12-05T08:35:09","date_gmt":"2017-12-05T16:35:09","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1112"},"modified":"2017-12-11T22:29:47","modified_gmt":"2017-12-12T06:29:47","slug":"leetcode-198-house-robber","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-198-house-robber\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 198. House Robber"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 198. House Robber - \u5237\u9898\u627e\u5de5\u4f5c EP124\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/H75Qp7ExCwo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and\u00a0<b>it will automatically contact the police if two adjacent houses were broken into on the same night<\/b>.<\/p>\n<p>Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight\u00a0<b>without alerting the police<\/b>.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n) -&gt; O(1)<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/198-ep124.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1118\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/198-ep124.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/198-ep124.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/198-ep124-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/198-ep124-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ Recursion + Memorization<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 0 ms\r\nclass Solution {\r\npublic:\r\n    int rob(vector&lt;int&gt;&amp; nums) {\r\n        const int n = nums.size();\r\n        m_ = vector&lt;int&gt;(n, -1);\r\n        return rob(nums, n - 1);\r\n    }\r\nprivate:\r\n    int rob(const vector&lt;int&gt;&amp; nums, int i) {\r\n        if (i &lt; 0) return 0;\r\n        if (m_[i] &gt;= 0) return m_[i];\r\n        return m_[i] = max(rob(nums, i - 2) + nums[i], \r\n                           rob(nums, i - 1));\r\n    }\r\n    \r\n    vector&lt;int&gt; m_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ \/ DP<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 3 ms\r\nclass Solution {\r\npublic:\r\n    int rob(vector&lt;int&gt;&amp; nums) {\r\n        if (nums.empty()) return 0;\r\n        vector&lt;int&gt; dp(nums.size(), 0);\r\n        for (int i = 0; i &lt; nums.size() ; ++i)\r\n            dp[i] = max((i &gt; 1 ? dp[i - 2] : 0) + nums[i], \r\n                        (i &gt; 0 ? dp[i - 1] : 0));\r\n        return dp.back();\r\n    }\r\n};<\/pre>\n<p>C++ \/ O(1) Space<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 3 ms\r\nclass Solution {\r\npublic:\r\n    int rob(vector&lt;int&gt;&amp; nums) {\r\n        if (nums.empty()) return 0;\r\n        int dp2 = 0;\r\n        int dp1 = 0;\r\n        for (int i = 0; i &lt; nums.size() ; ++i) {\r\n            int dp = max(dp2 + nums[i], dp1);\r\n            dp2 = dp1;\r\n            dp1 = dp;\r\n        }\r\n        return dp1;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,163],"tags":[18],"class_list":["post-1112","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-easy","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1112"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1112\/revisions"}],"predecessor-version":[{"id":1114,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1112\/revisions\/1114"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}