The Strategic Heist Challenge

Imagine you're planning the perfect heist along a street of consecutive houses, each containing different amounts of money. But there's a catch - you can't rob adjacent houses (the alarm systems are connected!).

Your goal isn't to maximize the total money stolen, but to minimize your "capability" - which is defined as the maximum amount stolen from any single house in your heist.

Given an array nums where nums[i] represents the money in house i, and an integer k representing the minimum number of houses you must rob, find the minimum possible capability across all valid robbery plans.

Example: Houses = [2,3,5,9], k=2
• Rob houses with [2,5] → capability = max(2,5) = 5
• Rob houses with [2,9] → capability = max(2,9) = 9
• Rob houses with [3,9] → capability = max(3,9) = 9
Answer: 5 (minimum capability)

Input & Output

example_1.py — Basic case
$ Input: nums = [2,3,5,9], k = 2
Output: 5
💡 Note: There are three ways to rob 2 houses: rob houses with indices [0,2], [0,3], or [1,3]. The first way gives us a capability of max(nums[0], nums[2]) = max(2, 5) = 5. The second way gives us a capability of max(nums[0], nums[3]) = max(2, 9) = 9. The third way gives us a capability of max(nums[1], nums[3]) = max(3, 9) = 9. Therefore, we return 5.
example_2.py — Minimum case
$ Input: nums = [2,7,9,3,1], k = 2
Output: 2
💡 Note: We can rob houses with indices [0,3] or [0,4]. For [0,3]: capability = max(2,3) = 3. For [0,4]: capability = max(2,1) = 2. We can also rob [1,4]: capability = max(7,1) = 7, or [2,4]: capability = max(9,1) = 9. The minimum capability is 2 from robbing houses [0,4].
example_3.py — All houses needed
$ Input: nums = [5,3,5,3], k = 3
Output: 3
💡 Note: The only way to rob 3 non-adjacent houses from 4 houses is to rob houses [0,2,3]. This gives us capability = max(5,5,3) = 5. Wait, that's wrong - we can't rob houses 2 and 3 as they're adjacent. We can rob houses [0,2] and need one more, so we must rob either house 1 or 3. If we rob [1,2], that's adjacent. The only valid ways for k=3 would require at least capability of 3 by robbing houses like [0,2] plus house 1, but [1,2] are adjacent. Actually, we can rob [1,3] + one more non-adjacent house 0, giving us [0,1,3] but [0,1] are adjacent. The maximum we can rob is 2 houses: [0,2] or [1,3]. Since k=3 > maximum possible (2), this case is invalid per constraints.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ (nums.length + 1) / 2
  • It is always possible to steal at least k houses

Visualization

Tap to expand
🎯 Binary Search StrategyLow RiskMedium RiskHigh RiskTesting Risk Level = 5Greedy Validation:2✓ ROB3SKIP5✓ ROB9TOO RISKYResult: Robbed 2 houses (need k=2) ✓Risk level 5 works! Try lower risk...Why This Works:• Binary search finds the minimum viable risk level• Greedy approach maximizes houses robbed at each risk level• Time: O(n log(max-min)), Space: O(1)Skip adjacent
Understanding the Visualization
1
Set Risk Range
Minimum risk = smallest house value, Maximum risk = largest house value
2
Try Medium Risk
Test if medium risk level allows robbing k houses
3
Greedy Validation
Go house by house: rob if value ≤ risk and previous wasn't robbed
4
Adjust Risk
If enough houses robbed, try lower risk; otherwise increase risk
Key Takeaway
🎯 Key Insight: Instead of trying all robbery combinations, we binary search on our 'risk tolerance' (maximum house value we'll steal) and greedily validate if that allows us to rob enough houses!
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 18
58.0K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen