def rob(nums):
a,b=0,0
for n in nums: a,b=b,max(b,a+n)
return b
public int rob(int[] nums){
int a=0,b=0;
for(int n:nums){int t=Math.max(b,a+n);a=b;b=t;}
return b;
}
Space-optimized. Simultaneous swap vs temp.
def rob(nums):
a,b=0,0
for n in nums: a,b=b,max(b,a+n)
return b
public int rob(int[] nums){
int a=0,b=0;
for(int n:nums){int t=Math.max(b,a+n);a=b;b=t;}
return b;
}
1. Track two previous values 2. Rob current or skip