> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# House Robber II Python Solution with Tests

> Tested Python solution for LeetCode 213 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 213, Medium. Topics: Array, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/house-robber-ii/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 213   # by problem number
lcpy gen -s house_robber_ii   # by problem name
```

## Problem

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

### Examples

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

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

```
Input: nums = [1,2,3]
Output: 3
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/house_robber_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/house_robber_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def rob(self, nums: list[int]) -> int:
        """
        Optimized version with better variable naming and edge case handling.

        Time: O(n)
        Space: O(1)
        """
        if not nums:
            return 0
        if len(nums) == 1:
            return nums[0]

        def rob_range(start: int, end: int) -> int:
            """Rob houses from start to end (inclusive)."""
            prev_rob = prev_not_rob = 0
            for i in range(start, end + 1):
                current_rob = prev_not_rob + nums[i]
                current_not_rob = max(prev_rob, prev_not_rob)
                prev_rob, prev_not_rob = current_rob, current_not_rob
            return max(prev_rob, prev_not_rob)

        n = len(nums)
        # Case 1: Rob houses 0 to n-2 (exclude last house)
        case1 = rob_range(0, n - 2)

        # Case 2: Rob houses 1 to n-1 (exclude first house)
        case2 = rob_range(1, n - 1)

        return max(case1, case2)
```

## Complexity

| Time | Space |
| ---- | ----- |
| -    | -     |

## Tags

[Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
