TIC
The Interns Company

House Robber II

MediumAcceptance: 38.5%

Problem Statement

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 an array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

Input Format:

  • An integer array nums representing the amount of money in each house

Output Format:

  • Return the maximum amount of money you can rob without alerting the police.

Examples:

Example 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 (circular arrangement). Instead, rob house 2 (money = 3).

Example 2:

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 3:

Input:

nums = [1,2,3]

Output:

3

Explanation:

Rob house 2 (money = 2) or rob house 3 (money = 3). We choose to rob house 3 for more money.

Solutions

Two-Pass Dynamic Programming

Time: O(n)
Space: O(1)

Break the circular arrangement by considering two scenarios: robbing houses 0 to n-2 (excluding the last house) or robbing houses 1 to n-1 (excluding the first house). Take the maximum result from these two scenarios.

Two-Pass DP Array Approach

Time: O(n)
Space: O(n)

Use dynamic programming arrays for both scenarios: excluding the first house and excluding the last house.

Algorithm Walkthrough

Example input: nums = [2,3,2]

Step-by-step execution:

  1. First scenario: Exclude the last house, consider nums = [2, 3]
  2. For nums = [2, 3]:
  3. prevMax = 2, currMax = max(2, 3) = 3
  4. Result for first scenario: 3
  5. Second scenario: Exclude the first house, consider nums = [3, 2]
  6. For nums = [3, 2]:
  7. prevMax = 3, currMax = max(3, 2) = 3
  8. Result for second scenario: 3
  9. Take the maximum of both scenarios: max(3, 3) = 3

Hints

Hint 1
The houses form a circle, which means the first and last houses are adjacent. How does this affect our approach?
Hint 2
Consider breaking the circle by not using the first house or not using the last house, then solving the linear version of the problem.
Hint 3
Apply the same dynamic programming approach as in House Robber, but with different subproblems.

Video Tutorial

Video tutorials can be a great way to understand algorithms visually

Visualization

Visualize the process of breaking the circular arrangement into two linear subproblems and solving each with dynamic programming.

Key visualization elements:

  • excluded house
  • current house
  • previous max profit
  • current max profit
  • decision made

Implementation Notes

This problem builds on the House Robber problem by adding a circular constraint. The key insight is to break the circle by considering two separate linear subproblems.