AlgoMaster Logo

House Robber II

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Recursive Solution with Memoization

Intuition:

In this problem, houses are arranged in a circle. This means the first house is a neighbor of the last house. To avoid robbing adjacent houses, the problem can be broken down into two subproblems:

  1. Rob houses from index 0 to n-2 (excluding the last house).
  2. Rob houses from index 1 to n-1 (excluding the first house).

Use a helper function to recursively calculate the maximum money that can be robbed, while using memoization to avoid redundant calculations.

Code:

2. Dynamic Programming with Optimized Space

Intuition:

This approach uses the same two subproblems as the previous method. However, we optimize the space complexity by storing only the last two results (as we only need these to calculate the current house's decision).

Code: