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:
Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 400When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are trying to find the absolute most money you can steal from a row of houses without setting off the security alarms by robbing two neighbors in a row. The brute force approach is to meticulously map out and test every single combination of houses you could possibly rob, ensuring you never pick two next-door neighbors.
Here's how the algorithm would work step-by-step:
def rob(houses):
def calculate_maximum_loot(current_house_index):
if current_house_index >= len(houses):
return 0
# Branch into two choices: either rob the current house or skip it entirely to explore all paths
loot_from_robbing_current = houses[current_house_index] + calculate_maximum_loot(current_house_index + 2)
loot_from_skipping_current = calculate_maximum_loot(current_house_index + 1)
# Compare both branches to find the maximum possible loot without triggering neighborhood alarms
return max(loot_from_robbing_current, loot_from_skipping_current)
return calculate_maximum_loot(0)Imagine you are walking down a street of houses, deciding which ones to target while keeping in mind that you cannot trigger alarms by hitting two adjacent properties. Instead of testing every single combination of houses, the smart strategy is to figure out the best possible total you can gather up to each house by looking only at the results of the houses immediately behind you.
Here's how the algorithm would work step-by-step:
def rob(house_values):
if not house_values:
return 0
if len(house_values) == 1:
return house_values[0]
maximum_loot_so_far = [0] * len(house_values)
maximum_loot_so_far[0] = house_values[0]
# Establish baseline best loot for the first two houses before iterating.
maximum_loot_so_far[1] = max(house_values[0], house_values[1])
for house_index in range(2, len(house_values)):
# Choose between skipping current house or robbing it with past loot.
maximum_loot_so_far[house_index] = max(
maximum_loot_so_far[house_index - 1],
maximum_loot_so_far[house_index - 2] + house_values[house_index]
)
# The final element contains the optimal solution for the entire street.
return maximum_loot_so_far[-1]| Case | How to Handle |
|---|---|
| Empty array of houses | Return zero immediately since no money can be robbed from an empty street. |
| Array with a single house | Return the single element's value as it is the maximum we can rob without triggering security. |
| Array with exactly two houses | Return the maximum of the two elements since we can only rob one of them. |
| All houses contain zero money | The algorithm will correctly compute and return zero total money robbed. |
| Houses with identical amounts of money | The dynamic programming recurrence relation naturally evaluates alternating choices to find the optimal sum. |
| Very large array of houses | An iterative dynamic programming approach with constant space complexity processes the input efficiently in linear time without stack overflow. |
| Array containing negative numbers | Assuming standard constraints money is non-negative, but if present, the recurrence relation would need to ensure we can skip negative houses. |
| Maximum possible integer values in the array | Using 64-bit integers for the DP state prevents potential integer overflow when summing large amounts. |