You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array
Output: 4
Explanation: You can rob house 1 (money = 1) and house 3 (money = 3).
So, the total amount will be 4.
Output: 12
Explanation: You can rob house 1 (money = 2), house 3 (money = 9) and house 5(money = 1)
So, the total amount will be 12
Output: 4
Explanation: You can rob house 1 (money = 2) and house 4 (money = 2)
So, the total amount will be 4
Contents
Solution 1 - using brute force approach Solution 2 - using DP memoization Solution 3 - using DP Bottom-Up approach using tabulation Solution 4 - using DP Bottom-Up approach memory optimized
In this approach, we will compute all possible combinations with conditions given in the problem, that is if we rob house at
-
We will create a function
bruteForceApproach that takesnums array as input argument. -
Create another helper function
bruteForceApproachRec that takenums andstart variable to indicate the start index of the sub array we should be considering.- Call this function recursively, by moving start to +2 index considering you want to rob current house and +1 index considering you dont want to rob the current house, and take the maximum out of these two possibilities.
- Base case will be, when it reaches end of array or goes out of bounds as we are trying with +1 and +2 index from every start index, we simply return 0.
Complexity Analysis:
Time complexity: Above code runs in O(2n) time where
Space complexity: O(n) as we are making recursive loops
From the above brute force approach, we can notice that there are some overlapping problems.
If we look at the decision tree diagram from above brute force aproach, highlighted sub problems are overlapping.
-
If we look at the decision tree diagram from above brute force aproach, highlighted sub problems are overlapping.
-
So, in this approach, we will be caching sub problem result into a
HashMap withstart index as key and its result as value. When a sub problem is repeated and found in cache, we will return it, otherwise we will call the function recursively and cache its result.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) for the cache we are maintaining and stack for recursive calls.
In this approach we will look at solving the problem using Dynamic programming using tabulation caching mechanism.
The time and memory complexity will be same as above solution using memoization,
but we can notice a pattern to improve memory complexity to constant in later solution.
Intution behind this solution is, when we are at
Base cases for this solution will be
-
dp[0] = nums[0] , because at 0th index, maximum amount you can rob is just that house. -
dp[1] = Max(nums[0], nums[1]) , because at 0th indexed house has higher amount, you can rob that house and skip index 1 house or rob index 1 house and skip index 0 house.
Finally, take the last element from
-
In the first step, if the input array
nums length is 1, just return the first element. -
In the first step, we will be initializing
dp array with same length asnums input array. -
Then, update
dp array with base cases. i.e.dp[0] = nums[0] anddp[1] = Max(nums[0], nums[1]) , so thedp array looks like this after applying base cases. dp = [1, 2, 0, 0]. -
For the next index
i=2 , maximum amount you can rob at this index will beMax(nums[2] + dp[0], dp[1]) so the dp array now will be dp = [1, 2, 4, 0] -
For the next index
i=3 , maximum amount you can rob at this index will beMax(nums[3] + dp[1], dp[2]) so the dp array now will be dp = [1, 2, 4,4 ] -
As you see, maximum amount is stored at last index, we can simply return that
dp[dp.length-1] .
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) since we have a
If we look at above DP solution using tabulation, when calculating maximum amount you can rob at index
-
In this approach, we will create two variables
m1 andm2 to hold previous values for i-2nd step and i-1st indexed houses. -
We will initialize
m1 = 0 andm1 = 0 . -
Next, for each index
i compute the maximum that can be robbed usingMax(nums[i]+m1, m2) , store this into a temporary variable, then make this value as newm2 and current m2 tom1 for next iteration.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1) since we are only using m1, m2 and a temp variables.
Above implementations source code can be found at