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 core interview patterns strategy.
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed and is protected by a security system with a color code.
You are given two integer arrays nums and colors, both of length n, where nums[i] is the amount of money in the ith house and colors[i] is the color code of that house.
You cannot rob two adjacent houses if they share the same color code.
Return the maximum amount of money you can rob.
Example 1:
Input: nums = [1,4,3,5], colors = [1,1,2,2]
Output: 9
Explanation:
i = 1 with nums[1] = 4 and i = 3 with nums[3] = 5 because they are non-adjacent.4 + 5 = 9.Example 2:
Input: nums = [3,1,2,4], colors = [2,3,2,2]
Output: 8
Explanation:
i = 0 with nums[0] = 3, i = 1 with nums[1] = 1, and i = 3 with nums[3] = 4.i = 0 and i = 1 have different colors, and house i = 3 is non-adjacent to i = 1.3 + 1 + 4 = 8.Example 3:
Input: nums = [10,1,3,9], colors = [1,1,1,2]
Output: 22
Explanation:
i = 0 with nums[0] = 10, i = 2 with nums[2] = 3, and i = 3 with nums[3] = 9.i = 0 and i = 2 are non-adjacent, and houses i = 2 and i = 3 have different colors.10 + 3 + 9 = 22.Constraints:
1 <= n == nums.length == colors.length <= 1051 <= nums[i], colors[i] <= 105Problem summary: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed and is protected by a security system with a color code. You are given two integer arrays nums and colors, both of length n, where nums[i] is the amount of money in the ith house and colors[i] is the color code of that house. You cannot rob two adjacent houses if they share the same color code. Return the maximum amount of money you can rob.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[1,4,3,5] [1,1,2,2]
[3,1,2,4] [2,3,2,2]
[10,1,3,9] [1,1,1,2]
house-robber)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3840: House Robber V
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3840: House Robber V
// package main
//
// // https://space.bilibili.com/206214
// func rob1(nums, colors []int) int64 {
// n := len(nums)
// f := make([]int, n+1)
// f[1] = nums[0]
// for i := 1; i < n; i++ {
// if colors[i] != colors[i-1] {
// f[i+1] = f[i] + nums[i]
// } else {
// f[i+1] = max(f[i-1]+nums[i], f[i]) // 选或不选
// }
// }
// return int64(f[n])
// }
//
// func rob(nums, colors []int) int64 {
// n := len(nums)
// f0, f1 := 0, nums[0]
// for i := 1; i < n; i++ {
// if colors[i] != colors[i-1] {
// f0 = f1
// f1 += nums[i]
// } else {
// f0, f1 = f1, max(f0+nums[i], f1)
// }
// }
// return int64(f1)
// }
// Accepted solution for LeetCode #3840: House Robber V
package main
// https://space.bilibili.com/206214
func rob1(nums, colors []int) int64 {
n := len(nums)
f := make([]int, n+1)
f[1] = nums[0]
for i := 1; i < n; i++ {
if colors[i] != colors[i-1] {
f[i+1] = f[i] + nums[i]
} else {
f[i+1] = max(f[i-1]+nums[i], f[i]) // 选或不选
}
}
return int64(f[n])
}
func rob(nums, colors []int) int64 {
n := len(nums)
f0, f1 := 0, nums[0]
for i := 1; i < n; i++ {
if colors[i] != colors[i-1] {
f0 = f1
f1 += nums[i]
} else {
f0, f1 = f1, max(f0+nums[i], f1)
}
}
return int64(f1)
}
# Accepted solution for LeetCode #3840: House Robber V
# Time: O(n)
# Space: O(1)
# dp
class Solution(object):
def rob(self, nums, colors):
"""
:type nums: List[int]
:type colors: List[int]
:rtype: int
"""
dp = [0]*2
for i in xrange(len(nums)):
dp[i%2] = max(dp[(i-2)%2]+nums[i], dp[(i-1)%2]) if i-1 >= 0 and colors[i-1] == colors[i] else dp[(i-1)%2]+nums[i]
return dp[(len(nums)-1)%2]
# Time: O(n)
# Space: O(1)
# dp
class Solution2(object):
def rob(self, nums, colors):
"""
:type nums: List[int]
:type colors: List[int]
:rtype: int
"""
dp = [0]*2
for i in xrange(len(nums)):
dp[0], dp[1] = max(dp[0], dp[1]), (dp[0] if i-1 >= 0 and colors[i-1] == colors[i] else max(dp[0], dp[1]))+nums[i]
return max(dp[0], dp[1])
// Accepted solution for LeetCode #3840: House Robber V
// Rust example auto-generated from go 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 (go):
// // Accepted solution for LeetCode #3840: House Robber V
// package main
//
// // https://space.bilibili.com/206214
// func rob1(nums, colors []int) int64 {
// n := len(nums)
// f := make([]int, n+1)
// f[1] = nums[0]
// for i := 1; i < n; i++ {
// if colors[i] != colors[i-1] {
// f[i+1] = f[i] + nums[i]
// } else {
// f[i+1] = max(f[i-1]+nums[i], f[i]) // 选或不选
// }
// }
// return int64(f[n])
// }
//
// func rob(nums, colors []int) int64 {
// n := len(nums)
// f0, f1 := 0, nums[0]
// for i := 1; i < n; i++ {
// if colors[i] != colors[i-1] {
// f0 = f1
// f1 += nums[i]
// } else {
// f0, f1 = f1, max(f0+nums[i], f1)
// }
// }
// return int64(f1)
// }
// Accepted solution for LeetCode #3840: House Robber V
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3840: House Robber V
// package main
//
// // https://space.bilibili.com/206214
// func rob1(nums, colors []int) int64 {
// n := len(nums)
// f := make([]int, n+1)
// f[1] = nums[0]
// for i := 1; i < n; i++ {
// if colors[i] != colors[i-1] {
// f[i+1] = f[i] + nums[i]
// } else {
// f[i+1] = max(f[i-1]+nums[i], f[i]) // 选或不选
// }
// }
// return int64(f[n])
// }
//
// func rob(nums, colors []int) int64 {
// n := len(nums)
// f0, f1 := 0, nums[0]
// for i := 1; i < n; i++ {
// if colors[i] != colors[i-1] {
// f0 = f1
// f1 += nums[i]
// } else {
// f0, f1 = f1, max(f0+nums[i], f1)
// }
// }
// return int64(f1)
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.