# Crack the house robber problem

My previous post introduced the Knapsack problem. It states how to solve using the dynamic programming algorithm. Besides Knapsack, there’s one series of problems that you might encounter in a coding interview: the house robber problems. Today, let’s talk about it.

## Problem description

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 system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Examples:

Case 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.

From the description and examples, we know the robber has to find out the maximum profit for the given houses without stealing two adjacent houses. How to solve it?

## Solution

It seems complicated at the first glance. In fact, if we break it into subproblems, there are only two scenarios to consider: at the current house, will the robber rob it? There are two cases to consider:

• At house i, if the robber robs it, meaning that the robber didn’t rob the previous house i-1, the maximum profit could be profit at the house i-2 plus current house valuei
• At house i, if the robber doesn’t rob it, meaning that the previous house i-1 could be robbed, the maximum profit might happen at the house i-1

As the result, at house i, the maximum profit is determined by its previous houses: profit[i] = max(profit[i-2]+house[i], profit[i-1]) . To compare these two values, the best way is to create an array to store the profit.

Let’s take one example to walk through the process:

Input: nums = [2,7,9,3,1]

Code:

def rob(nums):
if not nums or len(nums) == 0:
return 0

res = nums[0]
profit = [0] * (len(nums) + 1)
profit[1] = nums[0]

for i in range(2, len(profit)):
profit[i] = max(nums[i-1] + profit[i-2], profit[i-1])
res = max(res, profit[i])

return res

Analysis:

Let n be the number of the given houses:

• Time complexity: O(n) since we iterate the nums once, and saved the result at an array.
• Space complexity: O(n) since we create an extra array with size n+1 to help to find the maximum profit.

## House Robber II

There’s a more challenging version of the house robber problem:

Description:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Examples:

Case 1:
Input:
nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.

Solution

When the houses are circular, it’s different from the previous problem because the first house and last house can’t be robbed at the same time. How to solve it?

In fact, we can divide it into two subproblems:

• Don’t rob the first house, what’s the maximum profit robbed?
• Don’t rob the last house, what’s the maximum profit robbed?

Each subproblem could be solved using the previous algorithm. The only part that needs to change is that for the don’t rob front case: profit[1] = 0. To record the maximum profit of two different cases, we’ll create two different arrays: notRobFront, and notRobTail . After iterating the houses, we’ll compare two arrays to find the maximum profit.

Code:

def rob(nums):
size = len(nums)

Analysis:

Let n be the number of the given houses:

• Time complexity: O(n) since we iterate the nums once, and saved the result at an array.
• Space complexity: O(2n) ≈ O(n) since we create two extra arrays with size n+1 to help to find the maximum profit.

## Reference

on the way to become a programmer

## More from Wangyy

on the way to become a programmer

## Apache Spark Optimization Techniques

Get the Medium app