|
| 1 | +# 198. House Robber |
| 2 | + |
| 3 | +- Difficulty: Easy. |
| 4 | +- Related Topics: Dynamic Programming. |
| 5 | +- Similar Questions: Maximum Product Subarray, House Robber II, Paint House, Paint Fence, House Robber III, Non-negative Integers without Consecutive Ones, Coin Path, Delete and Earn. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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 **it will automatically contact the police if two adjacent houses were broken into on the same night**. |
| 10 | + |
| 11 | +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 **without alerting the police**. |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | + |
| 15 | +``` |
| 16 | +Input: [1,2,3,1] |
| 17 | +Output: 4 |
| 18 | +Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). |
| 19 | + Total amount you can rob = 1 + 3 = 4. |
| 20 | +``` |
| 21 | + |
| 22 | +**Example 2:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: [2,7,9,3,1] |
| 26 | +Output: 12 |
| 27 | +Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). |
| 28 | + Total amount you can rob = 2 + 9 + 1 = 12. |
| 29 | +``` |
| 30 | + |
| 31 | +## Solution 1 |
| 32 | + |
| 33 | +```javascript |
| 34 | +/** |
| 35 | + * @param {number[]} nums |
| 36 | + * @return {number} |
| 37 | + */ |
| 38 | +var rob = function (nums) { |
| 39 | + return helper([], 0, nums); |
| 40 | +}; |
| 41 | + |
| 42 | +var helper = function (dp, i, nums) { |
| 43 | + if (i >= nums.length) return 0; |
| 44 | + if (dp[i] === undefined) { |
| 45 | + dp[i] = Math.max( |
| 46 | + helper(dp, i + 1, nums), |
| 47 | + nums[i] + helper(dp, i + 2, nums) |
| 48 | + ); |
| 49 | + } |
| 50 | + return dp[i]; |
| 51 | +}; |
| 52 | +``` |
| 53 | + |
| 54 | +**Explain:** |
| 55 | + |
| 56 | +nope. |
| 57 | + |
| 58 | +**Complexity:** |
| 59 | + |
| 60 | +* Time complexity : O(n). |
| 61 | +* Space complexity : O(n). |
| 62 | + |
| 63 | +## Solution 2 |
| 64 | + |
| 65 | +```javascript |
| 66 | +/** |
| 67 | + * @param {number[]} nums |
| 68 | + * @return {number} |
| 69 | + */ |
| 70 | +var rob = function (nums) { |
| 71 | + var len = nums.length; |
| 72 | + var dp = Array(len); |
| 73 | + for (var i = len - 1; i >= 0; i--) { |
| 74 | + dp[i] = Math.max( |
| 75 | + dp[i + 1] || 0, |
| 76 | + nums[i] + (dp[i + 2] || 0) |
| 77 | + ); |
| 78 | + } |
| 79 | + return dp[0] || 0; |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +**Explain:** |
| 84 | + |
| 85 | +nope. |
| 86 | + |
| 87 | +**Complexity:** |
| 88 | + |
| 89 | +* Time complexity : O(n). |
| 90 | +* Space complexity : O(n). |
| 91 | + |
| 92 | +## Solution 3 |
| 93 | + |
| 94 | +```javascript |
| 95 | +/** |
| 96 | + * @param {number[]} nums |
| 97 | + * @return {number} |
| 98 | + */ |
| 99 | +var rob = function (nums) { |
| 100 | + var len = nums.length; |
| 101 | + var dp = [0, 0]; |
| 102 | + for (var i = len - 1; i >= 0; i--) { |
| 103 | + dp = [Math.max(dp[0], nums[i] + dp[1]), dp[0]]; |
| 104 | + } |
| 105 | + return dp[0]; |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +**Explain:** |
| 110 | + |
| 111 | +nope. |
| 112 | + |
| 113 | +**Complexity:** |
| 114 | + |
| 115 | +* Time complexity : O(n). |
| 116 | +* Space complexity : O(1). |
0 commit comments