Tap here to switch tabs
Problem
Ask
Submissions
Solution

Solution: House Robber

Statement

Naive approach

One possible approach would be to use a recursive function that explores all possible paths of robbing the houses. Starting from the first house, we can either rob it or skip it. If we rob the first house, we move on to the third house, since we cannot rob adjacent houses. If we skip the first house, we move on to the second house.

This approach gives us the solution, but it’s computationally expensive, since the time complexity of this algorithm is O(2n)O(2^n) ...

Tap here to switch tabs
Problem
Ask
Submissions
Solution

Solution: House Robber

Statement

Naive approach

One possible approach would be to use a recursive function that explores all possible paths of robbing the houses. Starting from the first house, we can either rob it or skip it. If we rob the first house, we move on to the third house, since we cannot rob adjacent houses. If we skip the first house, we move on to the second house.

This approach gives us the solution, but it’s computationally expensive, since the time complexity of this algorithm is O(2n)O(2^n) ...