Algorithm
Problem Name: 213. House Robber II
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.
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.
Example 1:
Input: nums = [2,3,2] Output: 3 Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:
Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 3:
Input: nums = [1,2,3] Output: 3
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 1000
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int robx(int *m, int *nums, int numsSize) {
int k1 = 0, k2 = 0;
if (numsSize == 1) return nums[0];
if (numsSize == 2) return nums[0] > nums[1] ? nums[0] : nums[1];
if (m[2] == -1) {
m[2] = robx(&m[2], &nums[2], numsSize - 2);
}
k2 = nums[0] + m[2];
if (m[1] == -1) {
m[1] = robx(&m[1], &nums[1], numsSize - 1);
}
k1 = m[1];
return k1 > k2 ? k1 : k2;
}
int rob(int* nums, int numsSize) {
int *m, k1, k2;
if (numsSize == 0) return 0;
m = malloc(numsSize * sizeof(int));
//assert(m);
memset(m, -1, numsSize * sizeof(int));
k1 = robx(m, &nums[0], numsSize - 1); // exclude the last house
memset(m, -1, numsSize * sizeof(int));
k2 = robx(m, &nums[1], numsSize - 1); // exclude the first house
free(m);
return k1 > k2 ? k1 : k2;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int rob(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
return Math.max(robHelper(nums, 0, nums.length - 2), robHelper(nums, 1, nums.length - 1));
}
private int robHelper(int[] nums, int lower, int higher) {
int include = 0;
int exclude = 0;
for (int j = lower; j <= higher; j++) {
int includeIdx = include;
int excludeIdx = exclude;
include = excludeIdx + nums[j];
exclude = Math.max(excludeIdx, includeIdx);
}
return Math.max(include, exclude);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const rob = function(nums) {
if(nums.length === 0) return 0
if(nums.length < 3) return Math.max(...nums)
const startFromFirst = [0,nums[0]]
const startFromSecond = [0,0]
for(let i = 2; i <= nums.length; i++) {
startFromFirst[i] = Math.max(startFromFirst[i - 1], startFromFirst[i - 2] + nums[i - 1])
startFromSecond[i] = Math.max(startFromSecond[i - 1], startFromSecond[i - 2] + nums[i - 1])
}
return Math.max(startFromFirst[nums.length - 1], startFromSecond[nums.length])
};
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def dp(self, nums):
if len(nums) <= 2: return max(nums or [0])
nums[2] += nums[0]
for i in range(3, len(nums)): nums[i] += max(nums[i - 2], nums[i - 3])
return max(nums[-1], nums[-2])
def rob(self, nums):
return max(self.dp(nums[:-1]), self.dp(nums[1:])) if len(nums) != 1 else nums[0]
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with C# Programming
Code -
C# Programming
using System;
namespace LeetCode
{
public class _0213_HouseRobberII
{
public int Rob(int[] nums)
{
if (nums.Length == 0) return 0;
if (nums.Length == 1) return nums[0];
var max1 = Rob(nums, 0, nums.Length - 2);
var max2 = Rob(nums, 1, nums.Length - 1);
return Math.Max(max1, max2);
}
private int Rob(int[] nums, int start, int end)
{
int amount1 = 0, amount2 = 0;
for (int i = start; i <= end; i++)
{
var current = Math.Max(amount2 + nums[i], amount1);
amount2 = amount1;
amount1 = current;
}
return amount1;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output