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.
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 400Problem summary: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3,1]
[2,7,9,3,1]
maximum-product-subarray)house-robber-ii)paint-house)paint-fence)house-robber-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #198: House Robber
class Solution {
private Integer[] f;
private int[] nums;
public int rob(int[] nums) {
this.nums = nums;
f = new Integer[nums.length];
return dfs(0);
}
private int dfs(int i) {
if (i >= nums.length) {
return 0;
}
if (f[i] == null) {
f[i] = Math.max(nums[i] + dfs(i + 2), dfs(i + 1));
}
return f[i];
}
}
// Accepted solution for LeetCode #198: House Robber
func rob(nums []int) int {
n := len(nums)
f := make([]int, n)
for i := range f {
f[i] = -1
}
var dfs func(int) int
dfs = func(i int) int {
if i >= n {
return 0
}
if f[i] < 0 {
f[i] = max(nums[i]+dfs(i+2), dfs(i+1))
}
return f[i]
}
return dfs(0)
}
# Accepted solution for LeetCode #198: House Robber
class Solution:
def rob(self, nums: List[int]) -> int:
@cache
def dfs(i: int) -> int:
if i >= len(nums):
return 0
return max(nums[i] + dfs(i + 2), dfs(i + 1))
return dfs(0)
// Accepted solution for LeetCode #198: House Robber
impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
fn dfs(i: usize, nums: &Vec<i32>, f: &mut Vec<i32>) -> i32 {
if i >= nums.len() {
return 0;
}
if f[i] < 0 {
f[i] = (nums[i] + dfs(i + 2, nums, f)).max(dfs(i + 1, nums, f));
}
f[i]
}
let n = nums.len();
let mut f = vec![-1; n];
dfs(0, &nums, &mut f)
}
}
// Accepted solution for LeetCode #198: House Robber
function rob(nums: number[]): number {
const n = nums.length;
const f: number[] = Array(n).fill(-1);
const dfs = (i: number): number => {
if (i >= n) {
return 0;
}
if (f[i] < 0) {
f[i] = Math.max(nums[i] + dfs(i + 2), dfs(i + 1));
}
return f[i];
};
return dfs(0);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: 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.