My 32ms Python solution with O(1) space


  • 0
    S
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n == 0:
            return 0
        if n == 1:
            return nums[0]
        pre1 = nums[0]
        pre2 = 0
        for i in range(1,n):
            pre1, pre2 = max(pre1, pre2+nums[i]), pre1
        return pre1

Log in to reply
 

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