🧠 Robbery Optimization? This Greedy + Binary Search Trick Feels Like Cheating!
IntuitionYou’re asked to rob houses with a twist: you can’t rob adjacent ones, and you want to minimize the maximum amount you’ll steal from any single house —


### Intuition
You’re asked to rob houses with a twist: you can’t rob adjacent ones, and you want to **minimize the maximum amount you’ll steal from any single house** — while still robbing at least `k` of them.

This is not classic DP. Instead, you can flip the problem on its head and use **binary search** on the “capability” — the max amount you allow yourself to steal from any chosen house.

---

### Approach
![image.png](https://assets.leetcode.com/users/images/dd28358b-e04f-4ded-ba0f-1cc97001161a_1743533451.3823473.png)

1. Use binary search to find the **minimum capability** (max amount from any house) that allows us to rob at least `k` non-adjacent houses.
2. In the `can(threshold)` function, iterate greedily:
   - If `nums[i] <= threshold`, we "take" the house and skip the next (to avoid adjacency).
3. If we can select `k` or more such houses under the given threshold — try lowering it.
4. The lowest possible threshold that works is the answer.

---

### Complexity

**Time Complexity:**  
- ( O(n log m) ) — `n` = length of nums, `m` = range between min and max values.

**Space Complexity:**  
- ( O(1) ) — constant extra space.

---

### Code

```python []
class Solution(object):
    def minCapability(self, nums, k):
        lo = min(nums)
        hi = max(nums)
        def can(th):
            cnt = 0
            i = 0
            n = len(nums)
            while i < n:
                if nums[i] <= th:
                    cnt += 1
                    i += 2
                else:
                    i += 1
            return cnt >= k
        while lo < hi:
            mid = (lo + hi) // 2
            if can(mid):
                hi = mid
            else:
                lo = mid + 1
        return lo
```

```javascript []
var minCapability = function(nums, k) {
    let low = Math.min(...nums);
    let high = Math.max(...nums);

    const canRob = (threshold) => {
        let count = 0, i = 0;
        while (i < nums.length) {
            if (nums[i] <= threshold) {
                count++;
                i += 2;
            } else {
                i += 1;
            }
        }
        return count >= k;
    };

    while (low < high) {
        const mid = Math.floor((low + high) / 2);
        if (canRob(mid)) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }

    return low;
};
```

<img src="https://assets.leetcode.com/users/images/b91de711-43d7-4838-aabe-07852c019a4d_1743313174.0180438.webp" width="270"/>

['Array', 'Binary Search', 'Greedy', 'Python', 'JavaScript']