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

> Tested Python solution for LeetCode 2560 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 2560, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/house-robber-iv/description/).

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

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

## Problem

There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he **refuses to steal from adjacent homes**.

The **capability** of the robber is the maximum amount of money he steals from one house of all the houses he robbed.

You are given an integer array `nums` representing how much money is stashed in each house. More formally, the `i<sup>th</sup>` house from the left has `nums[i]` dollars.

You are also given an integer `k`, representing the **minimum** number of houses the robber will steal from. It is always possible to steal at least `k` houses.

Return *the **minimum** capability of the robber out of all the possible ways to steal at least* `k` *houses*.

### Examples

```
Input: nums = [2,3,5,9], k = 2
Output: 5
```

**Explanation:** There are three ways to rob at least 2 houses:

* Rob the houses at indices 0 and 2. Capability is max(nums\[0], nums\[2]) = 5.
* Rob the houses at indices 0 and 3. Capability is max(nums\[0], nums\[3]) = 9.
* Rob the houses at indices 1 and 3. Capability is max(nums\[1], nums\[3]) = 9.
  Therefore, we return min(5, 9, 9) = 5.

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

**Explanation:** There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums\[0], nums\[4]) = 2.

### Constraints

* 1 \<= nums.length \<= 10\<sup>5\</sup>
* 1 \<= nums\[i] \<= 10\<sup>9\</sup>
* 1 \<= k \<= (nums.length + 1)/2

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log m) where m = max(nums)
    # Space: O(1)
    def min_capability(self, nums: list[int], k: int) -> int:
        def can_steal(cap: int) -> bool:
            count = 0
            i = 0
            while i < len(nums):
                if nums[i] <= cap:
                    count += 1
                    i += 2
                else:
                    i += 1
            return count >= k

        lo, hi = min(nums), max(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            if can_steal(mid):
                hi = mid
            else:
                lo = mid + 1
        return lo
```

## Complexity

| Time | Space |
| - | - |
| O(n log m) where m = max(nums) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.