3840. House Robber V Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [3840] House Robber V
3 *
4 * 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.
5 * You are given two integer arrays nums and colors, both of length n, where nums[i] is the amount of money in the i^th house and colors[i] is the color code of that house.
6 * You cannot rob two adjacent houses if they share the same color code.
7 * Return the maximum amount of money you can rob.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [1,4,3,5], colors = [1,1,2,2]</span>
12 * Output: <span class="example-io">9</span>
13 * Explanation:
14 * 
15 * 	Choose houses i = 1 with nums[1] = 4 and i = 3 with nums[3] = 5 because they are non-adjacent.
16 * 	Thus, the total amount robbed is 4 + 5 = 9.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [3,1,2,4], colors = [2,3,2,2]</span>
21 * Output: <span class="example-io">8</span>
22 * Explanation:
23 * 
24 * 	Choose houses i = 0 with nums[0] = 3, i = 1 with nums[1] = 1, and i = 3 with nums[3] = 4.
25 * 	This selection is valid because houses i = 0 and i = 1 have different colors, and house i = 3 is non-adjacent to i = 1.
26 * 	Thus, the total amount robbed is 3 + 1 + 4 = 8.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">nums = [10,1,3,9], colors = [1,1,1,2]</span>
31 * Output: <span class="example-io">22</span>
32 * Explanation:
33 * 
34 * 	Choose houses i = 0 with nums[0] = 10, i = 2 with nums[2] = 3, and i = 3 with nums[3] = 9.
35 * 	This selection is valid because houses i = 0 and i = 2 are non-adjacent, and houses i = 2 and i = 3 have different colors.
36 * 	Thus, the total amount robbed is 10 + 3 + 9 = 22.
37 * </div>
38 *  
39 * Constraints:
40 * 
41 * 	1 <= n == nums.length == colors.length <= 10^5
42 * 	1 <= nums[i], colors[i] <= 10^5
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/house-robber-v/
48// discuss: https://leetcode.com/problems/house-robber-v/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn rob(nums: Vec<i32>, colors: Vec<i32>) -> i64 {
54        
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_3840() {
66    }
67}
68

Back
© 2026 bowen.ge All Rights Reserved.