|
| 1 | +# 198. House Robber |
| 2 | +LeetCode problem: [198. House Robber](https://leetcode.com/problems/house-robber/) |
| 3 | + |
| 4 | +## LeetCode problem description |
| 5 | +> 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 systems connected, and **it will automatically contact the police if two adjacent houses were broken into on the same night**. |
| 6 | +
|
| 7 | +Given an integer array `nums` representing the amount of money of each house, return the maximum amount of money you can rob tonight **without alerting the police**. |
| 8 | +``` |
| 9 | +------------------------------------------------------------------------------------------------- |
| 10 | +[Example 1] |
| 11 | +
|
| 12 | +Input: nums = [1,2,3,1] |
| 13 | +Output: 4 |
| 14 | +Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). |
| 15 | + Total amount you can rob = 1 + 3 = 4. |
| 16 | +------------------------------------------------------------------------------------------------- |
| 17 | +[Example 2] |
| 18 | +Input: nums = [2,7,9,3,1] |
| 19 | +Output: 12 |
| 20 | +Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). |
| 21 | + Total amount you can rob = 2 + 9 + 1 = 12. |
| 22 | +------------------------------------------------------------------------------------------------- |
| 23 | +[Constraints] |
| 24 | +
|
| 25 | +1 <= nums.length <= 100 |
| 26 | +0 <= nums[i] <= 400 |
| 27 | +------------------------------------------------------------------------------------------------- |
| 28 | +``` |
| 29 | + |
| 30 | +## Thoughts |
| 31 | +This problem can be solved using **Dynamic programming**. |
| 32 | + |
| 33 | +### Complexity |
| 34 | +* Time: `O(n)`. |
| 35 | +* Space: `O(n)`. |
| 36 | + |
| 37 | +## Python |
| 38 | +```python |
| 39 | +class Solution: |
| 40 | + def rob(self, nums: List[int]) -> int: |
| 41 | + if len(nums) == 1: |
| 42 | + return nums[0] |
| 43 | + |
| 44 | + dp = [0] * len(nums) |
| 45 | + dp[0] = nums[0] |
| 46 | + dp[1] = max(nums[0], nums[1]) |
| 47 | + |
| 48 | + for i in range(2, len(dp)): |
| 49 | + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) |
| 50 | + |
| 51 | + return dp[-1] |
| 52 | +``` |
| 53 | + |
| 54 | +## C++ |
| 55 | +```cpp |
| 56 | +class Solution { |
| 57 | +public: |
| 58 | + int rob(vector<int>& nums) { |
| 59 | + if (nums.size() == 1) { |
| 60 | + return nums[0]; |
| 61 | + } |
| 62 | + |
| 63 | + auto dp = vector<int>(nums.size()); |
| 64 | + dp[0] = nums[0]; |
| 65 | + dp[1] = max(nums[0], nums[1]); |
| 66 | + |
| 67 | + for (auto i = 2; i < dp.size(); i++) { |
| 68 | + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); |
| 69 | + } |
| 70 | + |
| 71 | + return dp.back(); |
| 72 | + } |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +## Java |
| 77 | +```java |
| 78 | +class Solution { |
| 79 | + public int rob(int[] nums) { |
| 80 | + if (nums.length == 1) { |
| 81 | + return nums[0]; |
| 82 | + } |
| 83 | + |
| 84 | + var dp = new int[nums.length]; |
| 85 | + dp[0] = nums[0]; |
| 86 | + dp[1] = Math.max(nums[0], nums[1]); |
| 87 | + |
| 88 | + for (var i = 2; i < dp.length; i++) { |
| 89 | + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); |
| 90 | + } |
| 91 | + |
| 92 | + return dp[dp.length - 1]; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## C# |
| 98 | +```c# |
| 99 | +public class Solution { |
| 100 | + public int Rob(int[] nums) { |
| 101 | + if (nums.Length == 1) { |
| 102 | + return nums[0]; |
| 103 | + } |
| 104 | + |
| 105 | + var dp = new int[nums.Length]; |
| 106 | + dp[0] = nums[0]; |
| 107 | + dp[1] = Math.Max(nums[0], nums[1]); |
| 108 | + |
| 109 | + for (var i = 2; i < dp.Length; i++) { |
| 110 | + dp[i] = Math.Max(dp[i - 1], dp[i - 2] + nums[i]); |
| 111 | + } |
| 112 | + |
| 113 | + return dp.Last(); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## JavaScript |
| 119 | +```javascript |
| 120 | +var rob = function(nums) { |
| 121 | + if (nums.length === 1) { |
| 122 | + return nums[0] |
| 123 | + } |
| 124 | + |
| 125 | + const dp = Array(nums.length).fill(0) |
| 126 | + dp[0] = nums[0] |
| 127 | + dp[1] = Math.max(nums[0], nums[1]) |
| 128 | + |
| 129 | + for (let i = 2; i < dp.length; i++) { |
| 130 | + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]) |
| 131 | + } |
| 132 | + |
| 133 | + return dp.at(-1) |
| 134 | +}; |
| 135 | +``` |
| 136 | + |
| 137 | +## Go |
| 138 | +```go |
| 139 | +func rob(nums []int) int { |
| 140 | + if len(nums) == 1 { |
| 141 | + return nums[0] |
| 142 | + } |
| 143 | + |
| 144 | + dp := make([]int, len(nums)) |
| 145 | + dp[0] = nums[0] |
| 146 | + dp[1] = max(nums[0], nums[1]) |
| 147 | + |
| 148 | + for i := 2; i < len(dp); i++ { |
| 149 | + dp[i] = max(dp[i-1], dp[i-2]+nums[i]) |
| 150 | + } |
| 151 | + |
| 152 | + return dp[len(dp)-1] |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## Ruby |
| 157 | +```ruby |
| 158 | +def rob(nums) |
| 159 | + return nums[0] if nums.size == 1 |
| 160 | + |
| 161 | + dp = Array.new(nums.size, 0) |
| 162 | + dp[0] = nums[0] |
| 163 | + dp[1] = [ nums[0], nums[1] ].max |
| 164 | + |
| 165 | + (2...dp.size).each do |i| |
| 166 | + dp[i] = [ dp[i - 1], dp[i - 2] + nums[i] ].max |
| 167 | + end |
| 168 | + |
| 169 | + dp[-1] |
| 170 | +end |
| 171 | +``` |
0 commit comments