Problem Description
Given an array of integers representing the amount of money in each house, determine the minimum possible "capability" (i.e., the maximum money stolen from a single house) such that the robber can rob at least k houses without robbing two adjacent houses.
Key Insights
- The robber must avoid robbing two consecutive houses.
- The "capability" is defined as the maximum amount taken from any robbed house.
- We need to select at least k houses.
- Binary search can be applied on the range of possible capabilities (from min(nums) to max(nums)).
- A greedy method is used to determine if a given candidate capability allows robbing at least k houses.
Space and Time Complexity
Time Complexity: O(n · log(max(nums)))
Space Complexity: O(1)
Solution
We use binary search over the possible range of capabilities. For each candidate capability (mid value), apply a greedy strategy:
- Iterate over the houses; if a house’s money is <= candidate, count it as robbed and skip the next house.
- If the total count of robbed houses is at least k, the candidate is valid.
- Adjust the binary search boundaries based on the validity of the candidate to converge to the minimal capability.
The main idea is to narrow down the capability threshold while ensuring that the condition of robbing at least k houses without violating the adjacent rule holds.