Finding the Minimum Robbery Capability – A Smart Approach

Imagine a street with several houses, each containing a certain amount of money. A robber wants to steal money, but there’s a catch – he cannot rob two adjacent houses. The goal is to find the minimum possible maximum amount he needs to be capable of stealing while ensuring he robs at least k houses.


Understanding the Problem

We are given:


nums: An array where nums[i] represents the money in the i-th house.

k: The minimum number of houses the robber must steal from.

We need to find the smallest possible "capability", meaning the highest amount the robber steals from any house in his plan.


How Do We Solve This Efficiently?

Since nums can be large (up to 10^5 houses), checking every combination would be too slow. Instead, we use Binary Search to efficiently find the answer.


Steps to Solve Using Binary Search

Set the Search Range


The lowest capability (l) is the minimum house value.

The highest capability (r) is the maximum house value.

Binary Search for the Minimum Capability


We test a midpoint capability and check if it’s possible to rob k houses without picking adjacent ones.

If possible, we try a smaller capability (move r = mid).

Otherwise, we increase the minimum capability (move l = mid + 1).

Continue Until l == r, which gives the smallest possible valid capability.


Code Implementation

python

Copy code

class Solution:

    def minCapability(self, nums, k):

        l, r = min(nums), max(nums) # Set binary search range

        

        def canRob(x):

            count, i = 0, 0

            while i < len(nums):

                if nums[i] <= x: # Only rob houses within capability x

                    count += 1

                    i += 1 # Skip next house to avoid adjacency

                i += 1

            return count >= k # Check if we robbed at least k houses


        while l < r:

            mid = (l + r) // 2

            if canRob(mid):

                r = mid # Try a smaller capability

            else:

                l = mid + 1 # Increase capability

        return l # The minimum valid capability

Example Walkthrough

Input:

python

Copy code

nums = [2, 3, 5, 9]

k = 2

Binary Search Steps

Step l r mid Can Rob k Houses? Action

1 2 9 5 ✅ Yes Move r = 5

2 2 5 3 ❌ No Move l = 4

3 4 5 4 ❌ No Move l = 5

End 5 5 - - ✅ Answer: 5

Output:

python

Copy code

5

The minimum capability required to rob at least 2 houses while skipping adjacent ones is 5.


Why Binary Search Works?

Instead of checking all possibilities (which is slow), we search in a sorted range.

The time complexity is O(N log M), where N is the number of houses, and M is the house values.

This makes it fast even for large inputs.

Final Thoughts

This method ensures the robber steals just enough to rob k houses while keeping his maximum risk (capability) as low as possible.


This binary search + greedy selection is a smart way to solve problems with constraints like "at least k items" and "non-adjacent selection".

Comments

Popular posts from this blog

Mastering the Difference Array Technique: A Simple Guide with Examples

Leetcode 2594. Minimum Time to Repair Cars explained clearly

Finding a Unique Binary String in Python