Simple ideal, wish to help others


  • 0
    M
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums or len(nums) == 0:
            return 0
        if len(nums) == 1:
            return nums[0]
        prev, cur = self.rob_others(nums[:len(nums)-1]), self.rob_others(nums[1:])
        return max(prev, cur)
        
    def rob_others(self, nums):
        n = len(nums); pre = 0; cur = 0
        for i in xrange(len(nums)):
            temp = max(pre + nums[i], cur);
            pre = cur
            cur = temp
        return cur

Log in to reply
 

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