> ## 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 Python Solution with Tests

> Tested Python solution for LeetCode 198 with 15 pytest cases. Generate a practice environment with lcpy.

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

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

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 198   # by problem number
lcpy gen -s house_robber   # 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, the only constraint stopping you from robbing each of them is that adjacent houses have security systems 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 = [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 = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
```

### Constraints

* `1 <= nums.length <= 100`
* `0 <= nums[i] <= 400`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    """
    Houses: [2, 7, 9, 3, 1]
    Can't rob adjacent houses!
    For each house: max(skip, rob) = max(prev1, prev2 + current)

    Step by step:
    i=0: prev2=0, prev1=0, num=2 → max(0, 0+2) = 2
    i=1: prev2=0, prev1=2, num=7 → max(2, 0+7) = 7
    i=2: prev2=2, prev1=7, num=9 → max(7, 2+9) = 11
    i=3: prev2=7, prev1=11, num=3 → max(11, 7+3) = 11
    i=4: prev2=11, prev1=11, num=1 → max(11, 11+1) = 12
    """

    # Time: O(n)
    # Space: O(1)
    def rob(self, nums: list[int]) -> int:
        prev2 = prev1 = 0  # prev2: max 2 ago, prev1: max 1 ago
        for num in nums:
            prev2, prev1 = prev1, max(prev1, prev2 + num)
        return prev1
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
