aboutsummaryrefslogtreecommitdiff
path: root/house_robber.py
blob: 1a13c0c948863291b99f7e298b23fd4f682b649f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 198. House Robber

"""
you are a professional robber planning to rob houses along a stree. each
house has a certain amount of money stashed, the only constraint topping you
from robbing each of them is that the adjacent houses have security systems
connected and it will automatically contact the police if two adjacent houses
were broken into on the same night. given an integer array 'nums'
representing the amount of money each house has, return the maximum amount of
money you can rob without alerting the police.
"""


class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 1:
            return nums[0]
        dp = [*nums]
        dp[1] = max(nums[0], nums[1])
        for i in range(2, len(nums)):
            dp[i] = max(dp[i - 1], nums[i] + dp[i - 2])
        return dp[-1]


if __name__ == "__main__":
    obj = Solution()
    print(obj.rob([1, 2, 3, 1]))
    print(obj.rob([2, 7, 9, 3, 1]))