Colored Security

You are a thief targeting a line of houses. Each house contains some cash nums[i] and is wired with an alarm system identified by a specific color code colors[i].

The twist on the classic rule: You are allowed to rob adjacent houses, but only if their alarm color codes are completely different. If two adjacent houses share the exact same color code, the alarm will trigger if you hit both, meaning you are forced to skip one.

Return the maximum amount of cash you can successfully extract.

Example 1:

Input: nums = [5, 5, 5, 5], colors = [1, 2, 1, 2]

Output: 20

Explanation: Since no two adjacent houses share the same color code (the colors strictly alternate between 1 and 2), the adjacency restriction is never triggered. This means we are entirely free to rob every single house on the street. Selecting all four houses yields a maximum possible total of 5 + 5 + 5 + 5 = 20.

Example 2:

Input: nums = [10, 20, 30], colors = [1, 1, 1]

Output: 40

Explanation: In this scenario, all three houses share the exact same color code. Because of this, we cannot rob any adjacent houses, reverting entirely to standard house robber rules. To maximize our profit, we must skip the middle house and rob the first and last houses (indices 0 and 2), giving us a total of 10 + 30 = 40.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [8, 10, 5, 7], colors = [3, 3, 4, 4]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n == nums.length == colors.length <= 105
  • 1 <= nums[i], colors[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long rob(vector<int>& nums, vector<int>& colors) {
// Write your code here
}
};
Test Case

Input:

Colors
Nums