This approach involves splitting the problem into two scenarios: robbing from house 0 to house n-2, and from house 1 to house n-1. The aim is to apply the linear version of the House Robber problem to each scenario separately and then take the maximum of the two resulting summed values.
Time Complexity: O(n).
Space Complexity: O(1).
1
2def robLinear(nums):
3 prev1, prev2 = 0, 0
4 for num in nums:
5 temp = prev1
6 prev1 = max(num + prev2, prev1)
7 prev2 = temp
8 return prev1
9
10class Solution:
11 def rob(self, nums):
12 if len(nums) == 1:
13 return nums[0]
14 return max(robLinear(nums[:-1]), robLinear(nums[1:]))
15
This Python solution utilizes a helper function robLinear
to handle the linear robbery problem. It then applies this helper to the range excluding first and last houses respectively, returning the greatest result.
This approach implements a memoized version of the dynamic programming solution to avoid recomputing values. We explore two scenarios of the circle, taking into account the cycles explicitly by caching intermediate results.
Time Complexity: O(n).
Space Complexity: O(n) for the memoization array.
1
2class Solution {
3 private int[] memo;
4 private int robHelper(int[] nums, int start, int end) {
5 if (start >= end) return 0;
6 if (memo[start] >= 0) return memo[start];
7 memo[start] = Math.max(nums[start] + robHelper(nums, start + 2, end), robHelper(nums, start + 1, end));
8 return memo[start];
9 }
10
11 public int rob(int[] nums) {
12 if (nums.length == 1) return nums[0];
13 memo = new int[nums.length];
14 Arrays.fill(memo, -1);
15 int case1 = robHelper(nums, 0, nums.length - 1);
16 Arrays.fill(memo, -1);
17 int case2 = robHelper(nums, 1, nums.length);
18 return Math.max(case1, case2);
19 }
20}
21
This Java solution uses an integer array memo
for caching operations to minimize redundant computations. It features a robHelper
private method to recursively determine the maximum amount drawable from specific index ranges. Each side of the circle is evaluated for maximizing results separately.