🔥 Optimized Heist: Binary Search +🔥 Greedy to Rob Wisely! 💡💸
Intuition 💡
We need to rob exactly k houses, ensuring no two adjacent ones are robbed, while minimizing the maximum money 💰 we take from any house.
Approach 🚀
B
# Intuition 💡

- We need to rob exactly k houses, ensuring no two adjacent ones are robbed, while minimizing the maximum money 💰 we take from any house.

---



# Approach 🚀
- **Binary Search** on [min(nums), max(nums)] to find the smallest possible max-robbery value.
- **Greedy Check**: Try to rob k houses where each house's value ≤ mid, skipping adjacent ones.
- **Adjust Search Space**:
-     If k houses can be robbed ✅ → Try a smaller mid (left).
-     Else ❌ → Try a larger mid (right).

# Complexity ⏳
-     Time: O(n log(max - min)) (Binary Search + Greedy Check).
-     Space: O(1) (Constant extra space)

# Code
```cpp []
class Solution {
public:
    int minCapability(vector<int>& nums, int k) {
        int start = INT_MAX;
        int end = INT_MIN;

        for (int num : nums) {
            if (num > end)
                end = num;
            if (num < start)
                start = num;
        }

        int ans = 0;
        while (start <= end) {
            int mid = start + (end - start) / 2;

            if (isValidToRobb(mid, nums, k)) {
                ans = mid;
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return ans;
    }

private:
    bool isValidToRobb(int mid, vector<int>& num, int k) {
        int cnt = 0;
        for (int i = 0; i < num.size(); i++) {
            if (num[i] <= mid) {
                cnt++;
                i++;
            }
            if (cnt >= k)
                return true;
        }
        return false;
    }
};
```

```java []
import java.util.*;

class Solution {
    public int minCapability(int[] nums, int k) {
        int start = Integer.MAX_VALUE;
        int end = Integer.MIN_VALUE;

        for (int num : nums) {
            start = Math.min(start, num);
            end = Math.max(end, num);
        }

        int ans = 0;
        while (start <= end) {
            int mid = start + (end - start) / 2;

            if (isValidToRob(mid, nums, k)) {
                ans = mid;
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return ans;
    }

    private boolean isValidToRob(int mid, int[] nums, int k) {
        int cnt = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= mid) {
                cnt++;
                i++;
            }
            if (cnt >= k)
                return true;
        }
        return false;
    }
}

```
```python []
class Solution:
    def minCapability(self, nums, k):
        start, end = min(nums), max(nums)
        ans = 0

        while start <= end:
            mid = (start + end) // 2
            if self.isValidToRob(mid, nums, k):
                ans = mid
                end = mid - 1
            else:
                start = mid + 1
        return ans

    def isValidToRob(self, mid, nums, k):
        cnt, i = 0, 0
        while i < len(nums):
            if nums[i] <= mid:
                cnt += 1
                i += 1  # Skip next house
            i += 1
            if cnt >= k:
                return True
        return False

```
```ruby []
class Solution
    def min_capability(nums, k)
        start, end_value = nums.min, nums.max
        ans = 0

        while start <= end_value
            mid = (start + end_value) / 2
            if is_valid_to_rob(mid, nums, k)
                ans = mid
                end_value = mid - 1
            else
                start = mid + 1
            end
        end
        ans
    end

    def is_valid_to_rob(mid, nums, k)
        cnt = 0
        i = 0

        while i < nums.length
            if nums[i] <= mid
                cnt += 1
                i += 1  # Skip next house
            end
            i += 1
            return true if cnt >= k
        end
        false
    end
end

```
```Rust []
impl Solution {
    pub fn min_capability(nums: Vec<i32>, k: i32) -> i32 {
        let mut start = *nums.iter().min().unwrap();
        let mut end = *nums.iter().max().unwrap();
        let mut ans = 0;

        while start <= end {
            let mid = start + (end - start) / 2;

            if Self::is_valid_to_rob(mid, &nums, k) {
                ans = mid;
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        ans
    }

    fn is_valid_to_rob(mid: i32, nums: &Vec<i32>, k: i32) -> bool {
        let mut cnt = 0;
        let mut i = 0;

        while i < nums.len() {
            if nums[i] <= mid {
                cnt += 1;
                i += 1; // Skip next house
            }
            i += 1;
            if cnt >= k {
                return true;
            }
        }
        false
    }
}

```
```Javascript []
class Solution {
    minCapability(nums, k) {
        let start = Math.min(...nums);
        let end = Math.max(...nums);
        let ans = 0;

        while (start <= end) {
            let mid = Math.floor((start + end) / 2);
            if (this.isValidToRobb(mid, nums, k)) {
                ans = mid;
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return ans;
    }

    isValidToRobb(mid, nums, k) {
        let count = 0;
        for (let i = 0; i < nums.length; i++) {
            if (nums[i] <= mid) {
                count++;
                i++; // Skip adjacent house
            }
            if (count >= k) return true;
        }
        return false;
    }
}


```
```Kotlin []
class Solution {
    fun minCapability(nums: IntArray, k: Int): Int {
        var start = nums.min()
        var end = nums.max()
        var ans = 0

        while (start <= end) {
            val mid = start + (end - start) / 2
            if (isValidToRobb(mid, nums, k)) {
                ans = mid
                end = mid - 1
            } else {
                start = mid + 1
            }
        }
        return ans
    }

    private fun isValidToRobb(mid: Int, nums: IntArray, k: Int): Boolean {
        var count = 0
        var i = 0

        while (i < nums.size) {
            if (nums[i] <= mid) {
                count++
                i++ // Skip adjacent house
            }
            i++
            if (count >= k) return true
        }
        return false
    }
}

```


['Array', 'Binary Search', 'Python', 'C++', 'Java', 'Rust', 'Ruby', 'Kotlin', 'JavaScript']