Leetcode!
  • LeetCode!
  • My personal guide to Leetcode
  • Array
    • 11. Container With Most Water
    • 15. 3Sum
    • 219. Contains Duplicate II
    • 238. Product of Array Except Self
    • 245. Shortest Word Distance III
    • 392. Is Subsequence
    • 442. Find All Duplicates in an Array
    • 561. Array Partition I
    • 661. Image Smoother
    • 697. Degree of an Array
    • 723. Candy Crush
    • 832. Flipping an Image
  • Backtracking
    • 294. Flip Game II
    • 401. Binary Watch
    • 1079. Letter Tile Possibilities
  • Binary Search
    • 744. Find Smallest Letter Greater Than Target
    • 852. Peak Index in a Mountain Array
  • String
    • 6. ZigZag Conversion
    • 17. Letter Combinations of a Phone Number
    • 38. Count and Say
    • 521. Longest Uncommon Subsequence I
    • 544. Output Contest Matches
    • 937. Reorder Data in Log Files
  • Design
    • 251. Flatten 2D Vector
    • 281. Zigzag Iterator
  • Hash Table
    • 1. Two Sum
    • 508. Most Frequent Subtree Sum
    • 884. Uncommon Words from Two Sentences
    • 734. Sentence Similarity
  • Linked List
    • 2. Add Two Numbers
  • Unsorted
    • 917. Reverse Only Letters
  • Math
    • 43. Multiply Strings
    • 462. Minimum Moves to Equal Array Elements II
    • 553. Optimal Division
    • 800. Similar RGB Color
  • Greedy
    • 406. Queue Reconstruction by Height
    • 861. Score After Flipping Matrix
  • Tree
    • 701. Insert into a Binary Search Tree
    • 897. Increasing Order Search Tree
  • Divide and Conquer
    • 53. Maximum Subarray
  • Dynamic Programming
    • 62. Unique Paths
    • 63. Unique Paths II
    • 64. Minimum Path Sum
    • 120. Triangle
    • 198. House Robber
    • 213. House Robber II
    • 303. Range Sum Query - Immutable
    • 518. Coin Change 2
    • 750. Number Of Corner Rectangles
    • 337. House Robber III
  • Depth first search
    • 841. Keys and Rooms
  • Trie
    • 677. Map Sum Pairs
  • Two pointer
    • 42. Trapping Rain Water
Powered by GitBook
On this page
  1. Dynamic Programming

213. House Robber II

Approach 1: Keep track of amount robbed using DP

The approach is very similar to House Robber (which I strongly recommend reading first)

Since the array is circular this time, we will keep track of two lists of sums, one with nums[0] (dpl) and one without nums[0] (dpr). This way when we reach the last element, we will only check the array that does not have nums[0] to make sure that first and the last houses are not robbed together.

class Solution(object):
    def rob(self, nums):
        """
        https://leetcode.com/problems/house-robber-ii/description/
        :type nums: List[int]
        :rtype: int
        """
        lea = len(nums)
        if lea == 0:
            return 0
        if lea == 1:
            return nums[0]
        if lea == 2:
            return max(nums)
        if lea == 3:
            return max(nums[0], nums[1], nums[2])
        if lea == 4:
            return max(nums[1] + nums[3], nums[0] + nums[2])
        dpl = [0] * lea
        dpl[0], dpl[1], dpl[2], dpl[3] = nums[0], nums[1], nums[0] + nums[2], nums[3] + nums[0]
        dpr = [0] * lea
        dpr[0], dpr[1], dpr[2], dpr[3] = nums[0], nums[1], nums[2], nums[3] + nums[1]
        for i in range(4, lea-1):
            dpl[i] = nums[i] + max(dpl[i-2], dpl[i-3])
            dpr[i] = nums[i] + max(dpr[i-2], dpr[i-3])
        dpr[-1] = nums[-1] + max(dpr[-4], dpr[-3])
        return max(dpl[-2], dpl[-3], dpr[-1], dpr[-2])

Another added difference between this and House Robber I is that our base case has expanded from 3 to 4, because this time we are storing dpl[3] as nums[3] + nums[0] and dpr[3] as nums[3] + nums[1] to make sure dpl contains the first element while dpr does not.

Time Complexity: O(N)

Space Complexity: O(N)

Previous198. House RobberNext303. Range Sum Query - Immutable

Last updated 6 years ago