3840. House Robber V
February 26, 2026 · View on GitHub
Description
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:
- Choose houses
i = 1withnums[1] = 4andi = 3withnums[3] = 5because they are non-adjacent. - Thus, the total amount robbed is
4 + 5 = 9.
Example 2:
Input: nums = [3,1,2,4], colors = [2,3,2,2]
Output: 8
Explanation:
- Choose houses
i = 0withnums[0] = 3,i = 1withnums[1] = 1, andi = 3withnums[3] = 4. - This selection is valid because houses
i = 0andi = 1have different colors, and housei = 3is non-adjacent toi = 1. - Thus, the total amount robbed is
3 + 1 + 4 = 8.
Example 3:
Input: nums = [10,1,3,9], colors = [1,1,1,2]
Output: 22
Explanation:
- Choose houses
i = 0withnums[0] = 10,i = 2withnums[2] = 3, andi = 3withnums[3] = 9. - This selection is valid because houses
i = 0andi = 2are non-adjacent, and housesi = 2andi = 3have different colors. - Thus, the total amount robbed is
10 + 3 + 9 = 22.
Constraints:
1 <= n == nums.length == colors.length <= 1051 <= nums[i], colors[i] <= 105
Solutions
Solution 1: Dynamic Programming
We define two variables and , where represents the maximum amount when the current house is not robbed, and represents the maximum amount when the current house is robbed. Initially, and . The answer is .
Next, we traverse starting from the second house:
- If the current house has the same color as the previous house, then is updated to , and is updated to .
- If the current house has a different color from the previous house, then is updated to , and is updated to .
Finally, return .
The time complexity is , where is the number of houses. The space complexity is .
Python3
class Solution:
def rob(self, nums: List[int], colors: List[int]) -> int:
n = len(nums)
f, g = 0, nums[0]
for i in range(1, n):
if colors[i - 1] == colors[i]:
f, g = max(f, g), f + nums[i]
else:
f, g = max(f, g), max(f, g) + nums[i]
return max(f, g)
Java
class Solution {
public long rob(int[] nums, int[] colors) {
int n = nums.length;
long f = 0, g = nums[0];
for (int i = 1; i < n; i++) {
if (colors[i - 1] == colors[i]) {
long gg = f + nums[i];
f = Math.max(f, g);
g = gg;
} else {
long gg = Math.max(f, g) + nums[i];
f = Math.max(f, g);
g = gg;
}
}
return Math.max(f, g);
}
}
C++
class Solution {
public:
long long rob(vector<int>& nums, vector<int>& colors) {
int n = nums.size();
long long f = 0, g = nums[0];
for (int i = 1; i < n; i++) {
if (colors[i - 1] == colors[i]) {
long long gg = f + nums[i];
f = max(f, g);
g = gg;
} else {
long long gg = max(f, g) + nums[i];
f = max(f, g);
g = gg;
}
}
return max(f, g);
}
};
Go
func rob(nums []int, colors []int) int64 {
n := len(nums)
var f int64 = 0
var g int64 = int64(nums[0])
for i := 1; i < n; i++ {
if colors[i-1] == colors[i] {
f, g = max(f, g), f+int64(nums[i])
} else {
f, g = max(f, g), max(f, g)+int64(nums[i])
}
}
return max(f, g)
}
TypeScript
function rob(nums: number[], colors: number[]): number {
const n = nums.length;
let f = 0;
let g = nums[0];
for (let i = 1; i < n; i++) {
if (colors[i - 1] === colors[i]) {
[f, g] = [Math.max(f, g), f + nums[i]];
} else {
[f, g] = [Math.max(f, g), Math.max(f, g) + nums[i]];
}
}
return Math.max(f, g);
}
Rust
impl Solution {
pub fn rob(nums: Vec<i32>, colors: Vec<i32>) -> i64 {
let n = nums.len();
let mut f: i64 = 0;
let mut g: i64 = nums[0] as i64;
for i in 1..n {
if colors[i - 1] == colors[i] {
let gg = f + nums[i] as i64;
f = f.max(g);
g = gg;
} else {
let gg = f.max(g) + nums[i] as i64;
f = f.max(g);
g = gg;
}
}
f.max(g)
}
}