House Robber
Array, Dynamic Programming
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 adjacent 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 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:
Example 2:
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 400
Solutions
The key insight to solve this problem is to realize that the robber has two options at each house: rob it or skip it.
Rob it: If the robber decides to rob a house, they can't rob the previous house because of the connected security systems. So, the maximum amount of money they can get is the money in the current house plus the maximum money they can get from robbing the houses before the previous house.
Skip it: If the robber decides to skip a house, they can rob the next house. So, the maximum amount of money they can get is the maximum money they can get from robbing the previous house.
At each house, the robber will make the decision that gives them the most money. They will continue this process for all the houses along the street. In the end, the maximum amount of money the robber can rob is the maximum money they can get from the last house.
This problem can be solved using different techniques like dynamic programming, brute force, etc., but the core idea remains the same.
Approach – Brute Force Technique
The brute force approach involves generating all possible combinations of non-adjacent houses to rob and calculating the total amount of money for each combination.
Steps
Base Case: If
i < 0
, it means we have considered all houses, so we return 0. This is the base case for our recursion.Recursive Case: For
i >= 0
(i.e., there are still houses that we haven't considered), we have two options:Rob the current house: If we rob the current house (
nums[i]
), we can't rob the previous house (nums[i - 1]
), but we can rob the house before that (nums[i - 2]
). So, we make a recursive call toRob(nums, i - 2)
and addnums[i]
.Skip the current house: If we skip the current house, we can rob the previous house. So, we make a recursive call to
Rob(nums, i - 1)
.
Return the Maximum: We return the maximum of the two options, which represents the maximum amount of money that can be robbed considering the first
i
houses.
Complexity
Time Complexity: O(2^n)
Auxiliary Space: O(1)
Approach – Dynamic Programming Technique
The optimized approach uses dynamic programming to store the maximum amount of money that can be robbed up to each house.
Steps
Sure, I'd be happy to explain the steps of this algorithm. This is an implementation of a solution to the house robber problem using dynamic programming. The goal is to find the maximum amount of money that can be robbed from non-adjacent houses.
Here are the steps:
Initialize: Set
sum
andprevSum
to 0.sum
will keep track of the maximum amount of money that can be robbed up to the current house, whileprevSum
will keep track of the maximum amount of money that can be robbed up to the previous house.Iterate through the array: For each element in the array (index
i
from 0 tonums.Length - 1
), do the following:Store the current
sum
in a temporary variabletemp
.Update
sum
to be the maximum of the sum of the current house's money and the maximum money from the previous house (prevSum + nums[i]
), and the currentsum
. This step essentially says, "consider the current house's money plus the maximum money from the house before the previous house, or keep the maximum money from the previous house."Update
prevSum
to be the value oftemp
(which is the oldsum
). This step moves thesum
from the current house to the next house.
Return the result: After going through all the houses, return
sum
which now contains the maximum amount of money that can be robbed.
Complexity
Time Complexity: O(n)
Auxiliary Space: O(1)
Last updated