213. House Robber II
Approach 1: Keep track of amount robbed using DP
The approach is very similar to House Robber (which I strongly recommend reading first)
Since the array is circular this time, we will keep track of two lists of sums, one with nums[0]
(dpl
) and one without nums[0]
(dpr
). This way when we reach the last element, we will only check the array that does not have nums[0]
to make sure that first and the last houses are not robbed together.
Another added difference between this and House Robber I is that our base case has expanded from 3 to 4, because this time we are storing dpl[3]
as nums[3] + nums[0]
and dpr[3]
as nums[3] + nums[1]
to make sure dpl
contains the first element while dpr
does not.
Time Complexity: O(N)
Space Complexity: O(N)
Last updated