Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: House Robber

Posted on January 10, 2018July 26, 2020 by braindenny

House Robber



Similar Problems:

  • LeetCode: House Robber II
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #dynamicprogramming, #houserobber, #kadane

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 system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution: dp + O(1) space
## https://code.dennyzhang.com/house-robber
## Basic Ideas: dynamic programming
##
## Similar to Kadane's algorithm.
## Track current's best candidate, which indicates not taking current item
##
## Complexity: Time O(n), Space O(1)
class Solution:
    def rob(self, nums: List[int]) -> int:
        n = len(nums)
        pp, res = 0, 0
        for i in range(n):
            tmp = res
            # don't take vs take
            res = max(res, pp+nums[i])
            pp = tmp # without current item
        return res

  • Solution: dp + O(1) space
## https://code.dennyzhang.com/house-robber
## Basic Ideas: dynamic programming
##
## Complexity: Time O(n), Space O(1)
class Solution:
    def rob(self, nums: List[int]) -> int:
        n = len(nums)
        if n==0: return 0
        if n==1: return nums[0]
        if n==2: return max(nums[0], nums[1])
        pp, p, v = nums[0], max(nums[1], nums[0]), 0
        res = 0
        for i in range(2, n):
            v = max(p, pp+nums[i])
            res = max(res, pp, p, v)
            pp, p = p, v
        return res

  • Solution: dp + O(n) space
## https://code.dennyzhang.com/house-robber
class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ## Idea: Recursive way will timeout
        ##       DP: robs[i] the max profit so far.
        ##       How does DP formula work?
        ## Complexity:
        length = len(nums)
        if length == 0:
            return 0
        if length == 1:
            return nums[0]
        if length == 2:
            return max(nums[0], nums[1])

        robs = [None]*length
        robs[0] = nums[0]
        robs[1] = max(nums[0], nums[1])
        robs[2] = max(nums[0]+nums[2], nums[1])

        for i in range(3, length):
            robs[i] = max(robs[i-3]+nums[i-1], robs[i-2]+nums[i])
        return robs[-1]
linkedin
github
slack

Post Views: 6
Posted in MediumTagged #dynamicprogramming, houserobber, kadane

Post navigation

LeetCode: Insert Delete GetRandom O(1)
LeetCode: Student Attendance Record II

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.