Same as House Robber, but the houses form a circle, so the first and last houses are neighbors.
The first and last houses are neighbors, so at most one is robbed. Solve the line without the last house and the line without the first, then take the better one.
1function rob(nums: number[]): number {2if (nums.length === 1) return nums[0];3const line = (lo: number, hi: number) => {4let prev2 = 0, prev1 = 0;5for (let i = lo; i <= hi; i++) {6[prev2, prev1] = [prev1, Math.max(prev1, prev2 + nums[i])];7}8return prev1;9};10const a = line(0, nums.length - 2);11const b = line(1, nums.length - 1);12return Math.max(a, b);13}
Without the last house: house 0 (2).
Space: play/pause · ←/→: step