Loading content...
You are a seasoned burglar strategizing a heist in a high-end neighborhood where all the residences are arranged in a circular loop. This means the first house is immediately adjacent to the last house in the layout. Each residence contains a certain amount of valuables waiting to be collected.
However, there's a critical constraint: each pair of neighboring houses shares a sophisticated interconnected alarm system. If any two adjacent properties are burglarized during the same night, the automated security network will immediately alert law enforcement.
Given an integer array valuables where valuables[i] represents the worth of items in the i-th residence, determine the maximum total value you can acquire in a single night without triggering any security alerts.
Key Insight: Since the houses form a closed loop, you must be especially careful about the relationship between the first and last houses—they are neighbors too!
valuables = [2,3,2]3You cannot collect from the first residence (valuables = 2) and then the third residence (valuables = 2), because they are neighbors in the circular arrangement. The optimal strategy is to only target the second residence, yielding a total of 3.
valuables = [1,2,3,1]4Target the first residence (valuables = 1) and the third residence (valuables = 3). Total collection = 1 + 3 = 4. Note that you cannot include the last residence because it's adjacent to the first one.
valuables = [1,2,3]3Target the second residence (valuables = 3). Alternatively, you could target the first one (valuables = 1) and third one (valuables = 3), but since they're neighbors in the circular layout, that's not allowed. The best single selection is 3.
Constraints