Skip to content

Latest commit

 

History

History
118 lines (86 loc) · 3.23 KB

0198. House Robber.md

File metadata and controls

118 lines (86 loc) · 3.23 KB

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that 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 of each house, return the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2:

Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.

Constraints:

1 <= nums.length <= 100 0 <= nums[i] <= 400

Solution 1

class Solution:
    def rob(nums):
        if not nums:
            return 0

        n = len(nums)
        if n == 1:
            return nums[0]
        if n == 2:
            return max(nums[0], nums[1])

        dp = [0] * n
        dp[0] = nums[0]
        dp[1] = max(nums[0], nums[1])

        for i in range(2, n):
            dp[i] = max(nums[i] + dp[i-2], dp[i-1])

        return dp[n-1]
  • simper 1:
class Solution:
    def rob(self, nums: List[int]) -> int:
        n = len(nums)
        dp = [0 for i in range(n)]
        for i in range(min(2,n)):
            dp[i] = max(nums[:i+1])

        for i in range(2, n):
            dp[i] = max(dp[i-2] + nums[i], dp[i-1])
        return dp[-1]
  • simper 2:
class Solution:
    def rob(self, nums: List[int]) -> int:
        l = len(nums)+1
        arr = [0 for i in range(l)]
        for i in range(1, l):
            arr[i] = max(arr[i-1], nums[i-1]+arr[i-2])
        return arr[-1]

Solution 2

class Solution:
    def rob(self, nums: List[int]) -> int:
        f = g = 0
        for x in nums:
            f, g = max(f, g), f + x
        return max(f, g)

Solution 3

class Solution:
    def rob(self, nums: List[int]) -> int:
        memo = {}

        def search(i, houses):
            if i > len(houses) - 1:
                return 0
            if i in memo:
                return memo[i]
            memo[i] = max(search(i+1, houses), houses[i] + search(i+2, houses))
            return memo[i]
        return search(0, nums)
  • memo[i]: the maximum amount of money that can be robbed starting from the current house index i.

  • search(i+1, houses): not to rob the current house (houses[i]) and move on to the next house (i+1). search(i+1, houses) calculates the maximum amount that can be robbed starting from the next house.

  • houses[i] + search(i+2, houses): to rob the current house (houses[i]) and then move two houses ahead to avoid robbing adjacent houses (i+2).

  • memo[i] = max(...): the maximum amount of money that can be robbed starting from the current house index i.