Clearest python way


  • 1
    C
    def rob(self, nums):
            # last, near, current
            #  |     |     |
            # n-2   n-1    n
            last = near = 0
            for i in xrange(len(nums)):
                # if rob current, the total money we got is last total + this(nums[i] + las)
                # if not, the total is still the same as the nearest house(near)
                current = max(nums[i] + last, near)
                last = near
                near = current
            return near
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.