|
| 1 | +# 213. House Robber II |
| 2 | +LeetCode problem: [213. House Robber II](https://leetcode.com/problems/house-robber-ii/) |
| 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. All houses at this place are **arranged in a circle**. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system 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 | +---------------------------------------------------------------------------------------------------------------------- |
| 11 | +[Example 1] |
| 12 | +
|
| 13 | +Input: nums = [2,3,2] |
| 14 | +Output: 3 |
| 15 | +Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses. |
| 16 | +---------------------------------------------------------------------------------------------------------------------- |
| 17 | +[Example 2] |
| 18 | +
|
| 19 | +Input: nums = [1,2,3,1] |
| 20 | +Output: 4 |
| 21 | +Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). |
| 22 | + Total amount you can rob = 1 + 3 = 4. |
| 23 | +---------------------------------------------------------------------------------------------------------------------- |
| 24 | +[Example 3] |
| 25 | +
|
| 26 | +Input: nums = [1,2,3] |
| 27 | +Output: 3 |
| 28 | +---------------------------------------------------------------------------------------------------------------------- |
| 29 | +[Constraints] |
| 30 | +
|
| 31 | +1 <= nums.length <= 100 |
| 32 | +0 <= nums[i] <= 1000 |
| 33 | +---------------------------------------------------------------------------------------------------------------------- |
| 34 | +``` |
| 35 | + |
| 36 | +## Thoughts |
| 37 | +This problem can be solved using **Dynamic programming**. |
| 38 | + |
| 39 | +Detailed solutions will be given later, and now only the best practices in 3 to 7 languages are given. |
| 40 | + |
| 41 | +### Complexity |
| 42 | +* Time: `O(n)`. |
| 43 | +* Space: `O(n)`. |
| 44 | + |
| 45 | +## Python |
| 46 | +### Solution 1 |
| 47 | +```python |
| 48 | +class Solution: |
| 49 | + def rob(self, nums: List[int]) -> int: |
| 50 | + if len(nums) <= 2: |
| 51 | + return max(nums) |
| 52 | + |
| 53 | + return max( |
| 54 | + max_money_robbed(nums[1:]), |
| 55 | + max_money_robbed(nums[:len(nums) - 1]) |
| 56 | + ) |
| 57 | + |
| 58 | +def max_money_robbed(nums): |
| 59 | + dp = [0] * len(nums) |
| 60 | + dp[0] = nums[0] |
| 61 | + dp[1] = max(nums[0], nums[1]) |
| 62 | + |
| 63 | + for i in range(2, len(dp)): |
| 64 | + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) |
| 65 | + |
| 66 | + return dp[-1] |
| 67 | +``` |
| 68 | + |
| 69 | +## C++ |
| 70 | +```cpp |
| 71 | + |
| 72 | +``` |
| 73 | + |
| 74 | +## Java |
| 75 | +```java |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +## C# |
| 80 | +```c# |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +## JavaScript |
| 85 | +```javascript |
| 86 | +var rob = function (nums) { |
| 87 | + if (nums.length <= 2) { |
| 88 | + return Math.max(...nums) |
| 89 | + } |
| 90 | + |
| 91 | + return Math.max( |
| 92 | + maxMoneyRobbed(nums.slice(1,)), |
| 93 | + maxMoneyRobbed(nums.slice(0, nums.length - 1)) |
| 94 | + ) |
| 95 | +}; |
| 96 | + |
| 97 | +var maxMoneyRobbed = function (nums) { |
| 98 | + const dp = Array(nums.length).fill(0) |
| 99 | + dp[0] = nums[0] |
| 100 | + dp[1] = Math.max(nums[0], nums[1]) |
| 101 | + |
| 102 | + for (let i = 2; i < dp.length; i++) { |
| 103 | + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]) |
| 104 | + } |
| 105 | + |
| 106 | + return dp.at(-1) |
| 107 | +}; |
| 108 | +``` |
| 109 | + |
| 110 | +## Go |
| 111 | +```go |
| 112 | +func rob(nums []int) int { |
| 113 | + if len(nums) <= 2 { |
| 114 | + return slices.Max(nums) |
| 115 | + } |
| 116 | + |
| 117 | + return max( |
| 118 | + maxMoneyRobbed(nums[1:]), |
| 119 | + maxMoneyRobbed(nums[:len(nums) - 1]), |
| 120 | + ) |
| 121 | +} |
| 122 | + |
| 123 | +func maxMoneyRobbed(nums []int) int { |
| 124 | + dp := make([]int, len(nums)) |
| 125 | + dp[0] = nums[0] |
| 126 | + dp[1] = max(nums[0], nums[1]) |
| 127 | + |
| 128 | + for i := 2; i < len(dp); i++ { |
| 129 | + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) |
| 130 | + } |
| 131 | + |
| 132 | + return dp[len(dp) - 1] |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Ruby |
| 137 | +```ruby |
| 138 | + |
| 139 | +``` |
0 commit comments