Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 ith 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.
Example 1:
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.
Example 2:
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 <= 1051 <= nums[i] <= 1091 <= k <= (nums.length + 1)/2Problem summary: 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 ith 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Dynamic Programming · Greedy
[2,3,5,9] 2
[2,7,9,3,1] 2
container-with-most-water)house-robber)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2560: House Robber IV
class Solution {
public int minCapability(int[] nums, int k) {
int left = 0, right = (int) 1e9;
while (left < right) {
int mid = (left + right) >> 1;
if (f(nums, mid) >= k) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private int f(int[] nums, int x) {
int cnt = 0, j = -2;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] > x || i == j + 1) {
continue;
}
++cnt;
j = i;
}
return cnt;
}
}
// Accepted solution for LeetCode #2560: House Robber IV
func minCapability(nums []int, k int) int {
return sort.Search(1e9+1, func(x int) bool {
cnt, j := 0, -2
for i, v := range nums {
if v > x || i == j+1 {
continue
}
cnt++
j = i
}
return cnt >= k
})
}
# Accepted solution for LeetCode #2560: House Robber IV
class Solution:
def minCapability(self, nums: List[int], k: int) -> int:
def f(x):
cnt, j = 0, -2
for i, v in enumerate(nums):
if v > x or i == j + 1:
continue
cnt += 1
j = i
return cnt >= k
return bisect_left(range(max(nums) + 1), True, key=f)
// Accepted solution for LeetCode #2560: House Robber IV
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2560: House Robber IV
// class Solution {
// public int minCapability(int[] nums, int k) {
// int left = 0, right = (int) 1e9;
// while (left < right) {
// int mid = (left + right) >> 1;
// if (f(nums, mid) >= k) {
// right = mid;
// } else {
// left = mid + 1;
// }
// }
// return left;
// }
//
// private int f(int[] nums, int x) {
// int cnt = 0, j = -2;
// for (int i = 0; i < nums.length; ++i) {
// if (nums[i] > x || i == j + 1) {
// continue;
// }
// ++cnt;
// j = i;
// }
// return cnt;
// }
// }
// Accepted solution for LeetCode #2560: House Robber IV
function minCapability(nums: number[], k: number): number {
const f = (mx: number): boolean => {
let cnt = 0;
let j = -2;
for (let i = 0; i < nums.length; ++i) {
if (nums[i] <= mx && i - j > 1) {
++cnt;
j = i;
}
}
return cnt >= k;
};
let left = 1;
let right = Math.max(...nums);
while (left < right) {
const mid = (left + right) >> 1;
if (f(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.