↓Skip to main content
  1. LeetCode/

LeetCode 3840: House Robber V

·2 mins· ·
LeetCode Medium Dynamic-Programming Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-14

  • Submissions: Accepted (first try)

Approach
#

dp[i] = the most money from the first i houses. For house i (0-indexed):

  • Different color from the previous house: taking it conflicts with nothing, and all amounts are positive, so take it: dp[i + 1] = dp[i] + nums[i].
  • Same color as the previous house: plain House Robber, taking it means skipping the previous one: dp[i + 1] = max(dp[i], dp[i - 1] + nums[i]).

My Solution
#

class Solution:
    def rob(self, nums: List[int], colors: List[int]) -> int:
        n = len(nums)
        dp = [0] * (n + 1)                         # dp[i]: best over the first i houses
        for i in range(n):
            if i > 0 and colors[i] == colors[i - 1]:
                dp[i + 1] = max(dp[i], dp[i - 1] + nums[i])
            else:
                dp[i + 1] = dp[i] + nums[i]        # no conflict, take it
        return dp[n]

Complexity
#

Time O(n), space O(n) (compressible to two variables).

Mistakes to Avoid
#

  • Treating it as ordinary House Robber: neighbors with different colors can both be taken.

Interview Explanation
#

dp[i] is the best over the first i houses. If this house’s color differs from the previous one, taking it causes no conflict and amounts are positive, so add it; only with matching colors is it House Robber, choosing between skipping this house and taking it while skipping the previous one. Time O(n).

Related

LeetCode 1493: Longest Subarray of 1's After Deleting One Element
·2 mins
LeetCode Medium Sliding-Window Dynamic-Programming Array
LeetCode 1493 note: find the longest window with at most one 0; since one element must be deleted, the answer is the window length minus 1
LeetCode 3638: Maximum Balanced Shipments
·2 mins
LeetCode Medium Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 3638 note: greedily cut a segment as soon as a value below its maximum appears; until then the segment is non-decreasing, so its maximum is the previous value
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 473: Matchsticks to Square
·2 mins
LeetCode Medium Array Dynamic-Programming Backtracking Bit-Manipulation
LeetCode 473 note: the total must be divisible by 4; place sticks from longest to shortest into one of four sides, skipping a side that would overflow or that equals a side already tried