Explanation on how this code works for House robber in Python : So I was browsing solutions on leetcode and found this for HouseRobber 1:


        last, now = 0, 0
        for i in nums:
            last, now = now, max(last + i, now)
        return now


I'm kind of confused on how the code itself executes with the max value. It's a bit ambiguous to me since I'm still fairly new to python and coming from C++, I wrote something like this:

    lastNum = 0 #last number we took in
        lastSpot = 0 #last spot we took the number from (index placeholder)
        totalSum = 0 #max amount we can take from the houses
        for i, element in enumerate(nums):
            if element > lastNum: #if our element is bigger than our last number, we check...
                if lastSpot != i - 1: #if our last spot was more than a space away AND our element is bigger, we take our index number.
                    if lastNum + nums[lastSpot] < lastNum + nums[i]:
                        lastNum = element
                        totalSum += element
                        lastSpot = i #update our last spot
                elif lastSpot == i - 1: #if our last spot is next to our current spot, we gotta' swap a few values now.
                    totalSum -= lastNum #we remove the last value we added since we found a bigger spot
                    totalSum += element
                    lastSpot = i
                    lastNum = element
            elif i == lastSpot + 2: #if we are more than one spot away from our last spot, we take the number anyway and add it.
                lastNum = element
                totalSum += element
            elif element < lastNum:
                if lastNum + nums[lastSpot] < lastNum + nums[i]:
                    lastNum = element
                    totalSum += element
                    totalSum += nums[i - 2]
                    lastSpot = i        
            else:   
                lastNum = element
        
        return totalSum

I understand that we need to validate the values against each other and if we pass a value, we check to see if it's bigger than our last held value plus our current, then decide to skip or take depending on if it's a spot next to our currently chosen value. But how does that execute within the small bit of python code? I know how the max function takes 2 values and returns the larger of two - but am I missing something? Shouldn't this also be returning values next to the same value? Thank you for your time!