101 Logo
onenoughtone

Code Implementation

House Robber II Implementation

Below is the implementation of the house robber ii:

solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Find the maximum amount of money you can rob without alerting the police.
* Dynamic Programming with Two Cases approach.
*
* @param {number[]} nums - Array of money in each house
* @return {number} - Maximum amount of money
*/
function rob(nums) {
const n = nums.length;
// Handle edge cases
if (n === 0) return 0;
if (n === 1) return nums[0];
if (n === 2) return Math.max(nums[0], nums[1]);
// Case 1: Rob houses from index 0 to n-2 (excluding the last house)
const dp1 = new Array(n - 1);
dp1[0] = nums[0];
dp1[1] = Math.max(nums[0], nums[1]);
for (let i = 2; i < n - 1; i++) {
dp1[i] = Math.max(dp1[i - 1], dp1[i - 2] + nums[i]);
}
// Case 2: Rob houses from index 1 to n-1 (excluding the first house)
const dp2 = new Array(n - 1);
dp2[0] = nums[1];
dp2[1] = Math.max(nums[1], nums[2]);
for (let i = 2; i < n - 1; i++) {
dp2[i] = Math.max(dp2[i - 1], dp2[i - 2] + nums[i + 1]);
}
return Math.max(dp1[n - 2], dp2[n - 2]);
}
/**
* Find the maximum amount of money you can rob without alerting the police.
* Space-Optimized Dynamic Programming approach.
*
* @param {number[]} nums - Array of money in each house
* @return {number} - Maximum amount of money
*/
function robOptimized(nums) {
const n = nums.length;
// Handle edge cases
if (n === 0) return 0;
if (n === 1) return nums[0];
if (n === 2) return Math.max(nums[0], nums[1]);
// Helper function to solve the original House Robber problem
function robRange(nums, start, end) {
let prev2 = 0;
let prev1 = 0;
for (let i = start; i <= end; i++) {
const current = Math.max(prev1, prev2 + nums[i]);
prev2 = prev1;
prev1 = current;
}
return prev1;
}
// Case 1: Rob houses from index 0 to n-2 (excluding the last house)
const result1 = robRange(nums, 0, n - 2);
// Case 2: Rob houses from index 1 to n-1 (excluding the first house)
const result2 = robRange(nums, 1, n - 1);
return Math.max(result1, result2);
}
// Test cases
console.log(rob([2, 3, 2])); // 3
console.log(rob([1, 2, 3, 1])); // 4
console.log(rob([1, 2, 3])); // 3
console.log(robOptimized([2, 3, 2])); // 3
console.log(robOptimized([1, 2, 3, 1])); // 4
console.log(robOptimized([1, 2, 3])); // 3

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that this is a variation of the House Robber problem with a circular arrangement, where the first and last houses are adjacent.
  2. Handle Edge Cases: Handle edge cases such as empty arrays, arrays with one element, and arrays with two elements.
  3. Break the Circle: Break the circular arrangement by considering two cases: (1) excluding the last house, and (2) excluding the first house.
  4. Apply Dynamic Programming: Apply the original House Robber algorithm to each case, using dynamic programming to find the maximum amount of money that can be robbed.
  5. Optimize Space Complexity: Optimize the space complexity by using a helper function that only keeps track of the previous two values, rather than using arrays to store all the dp values.
ProblemSolutionCode
101 Logo
onenoughtone