Design Gurus Logo
Blind 75

There are n houses built in a line. A thief wants to steal the maximum possible money from these houses. The only restriction the thief has is that he can't steal from two consecutive houses, as that would alert the security system. How should the thief maximize his stealing?

Problem Statement

Given a number array representing the wealth of n houses, determine the maximum amount of money the thief can steal without alerting the security system.

Example 1:

Input: {2, 5, 1, 3, 6, 2, 4}
Output: 15
Explanation: The thief should steal from houses 5 + 6 + 4

Example 2:

Input: {2, 10, 14, 8, 1}
Output: 18
Explanation: The thief should steal from houses 10 + 8

Constraints:

  • 1 <= wealth.length <= 100
  • 0 <= wealth[i] <= 400

Let's first start with a recursive brute-force solution.

Why this is a Fibonacci Numbers problem

What the question saysThe signal it matches
"determine the maximum amount of money the thief can steal"the wording is maximum you can collect
"he can't steal from two consecutive houses"each state looks back a fixed distance, here two
"There are n houses built in a line"the input is a line

This is the maximise a total, with a rule variant: one state is the best haul using the first i items.

The closest alternative. Greedy, by taking the largest houses first and skipping any that touch one already taken. It is wrong, and example 2 proves it. The wealth is 2, 10, 14, 8, 1. Greedy takes 14 first, which blocks both 10 and 8, and ends at 14 plus 2 plus 1, which is 17. The stated answer is 10 plus 8, which is 18.

The introduction calls this the most instructive problem in the chapter, and the reason is the shape of the choice. At each house there are exactly two options. Skip it and keep the best total from the previous house. Or take it and add the best total from two houses back. The larger of those two is the answer for this position, and no other case exists.

Basic Solution

For every house i, we have two options:

  1. Steal from the current house (i), skip one and steal from (i+2).
  2. Skip the current house (i), and steal from the adjacent house (i+1).

The thief should choose the one with the maximum amount from the above two options. So our algorithm will look like this:

Python3
Python3

The time complexity of the above algorithm is exponential O(2^n). The space complexity is O(n) which is used to store the recursion stack.

Top-down Dynamic Programming with Memoization

To resolve overlapping subproblems, we can use an array to store the already solved subproblems.

Here is the code:

Python3
Python3

Bottom-up Dynamic Programming

Let's try to populate our dp[] array from the above solution, working in a bottom-up fashion. As we saw in the above code, every findMaxStealRecursive() is the maximum of the two recursive calls; we can use this fact to populate our array.

Algorithm Walkthrough

Let's trace the first example, {2, 5, 1, 3, 6, 2, 4}. Move through the steps one at a time:

mediaLink

Step 1. Each cell in the table will hold the most that can be taken using only the houses up to that point. Standing at any house there are exactly two things you can do: rob it, which rules out the house immediately before it, or skip it and keep whatever the house before it managed. Every possible route through the street is those two choices repeated, which is why each cell only ever compares two numbers.

1 of 9

Code

Here is the code for our bottom-up dynamic programming approach:

Python3
Python3

The above solution has time and space complexity of O(n).

Memory optimization

We can optimize the space used in our previous solution. We don't need to store all the previous numbers up to n, as we only need two previous numbers to calculate the next number in the sequence. Let's use this fact to further improve our solution:

Python3
Python3

The above solution has a time complexity of O(n) and a constant space complexity O(1).

Fibonacci number pattern

We can clearly see that this problem follows the Fibonacci number pattern. The only difference is that every Fibonacci number is a sum of the two preceding numbers, whereas in this problem every number (total wealth) is the maximum of previous two numbers.

No code editor for this lesson
This lesson focuses on concepts and theory