Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • 198 House Robber
  • 213. House Robber II

Was this helpful?

  1. 13. Dynamic Programming

House Robber

198 House Robber

Given a list of non-negative integers represent the money in house, determine the maximum amount of money you can rob but not rob 2 continuous house

  • Space O(n)

    public int rob(int[] nums) {
      int len = nums.length;
      if(len == 0) return 0;
      if(len == 1) return nums[0];
    
      int[] dp = new int[len];
      dp[0] = nums[0];
      dp[1] = Math.max(nums[0], nums[1]);
      for(int i=2; i<len; i++){
          dp[i] = Math.max(dp[i-2]+nums[i], dp[i-1]);
      }
      return dp[len-1];
    }
  • Space O(1)

    public int rob(int[] nums) {
      int curMax = 0;
      int preMax = 0;
      for(int n: nums){
          int tmp = curMax;
          curMax = Math.max(curMax, preMax + n);
          preMax = tmp;
      }
      return curMax;
    }

213. House Robber II

Same as house robber, but the first and the last house are connected

  • Break the circle, find the max of dp[n-1], dp[n]

public int rob(int[] nums) {
    if(nums.length == 0) return 0;
    if(nums.length == 1) return nums[0];
    return Math.max(helper(nums, 0, nums.length-1), helper(nums, 1, nums.length)); 
}

private int helper(int[] nums, int start, int end){
    int preMax = 0, curMax = 0;
    for(int i=start; i<end; i++){
        int tmp = curMax;
        curMax = Math.max(curMax, preMax + nums[i]);
        preMax = tmp;
    }
    return curMax;
}
PreviousRolling ArrayNextBackpack

Last updated 4 years ago

Was this helpful?