Data Structures and Algorithms: House Robber I and II šŸŽÆ

beginner
6 min

Data Structures and Algorithms: House Robber I and II šŸŽÆ

Welcome to the exciting world of Data Structures and Algorithms! Today, we're diving into two classic problems: House Robber I and House Robber II. These problems will help you understand the concept of Dynamic Programming, a powerful algorithmic tool used to solve a wide range of problems efficiently.

House Robber I šŸ 

Problem Statement

In House Robber I, we have an array nums representing the amount of money in each house. You can only rob one consecutive sequence of houses, and the goal is to maximize the total amount of money you can steal.

Example

markdown
Input: nums = [1, 2, 3, 1] Output: 4 (Rob houses 1, 2, 3) Input: nums = [2, 7, 9, 3, 1] Output: 12 (Rob houses 2, 7, 9)

Solution

Here's a step-by-step approach to solve House Robber I:

  1. Initialize two variables dp[i] for the maximum amount of money you can steal from houses 0 to i.

  2. For each index i, there are two possibilities:

    • Either you rob the house at i (nums[i] + dp[i - 2]), or you don't (dp[i - 1]).
    • Choose the maximum between the two possibilities: dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]).
  3. Finally, the maximum amount of money you can steal is dp[n-1], where n is the length of the array.

Code Example

python
def rob(nums): if not nums: return 0 if len(nums) == 1: return nums[0] dp = [0] * len(nums) dp[0], dp[1] = nums[0], max(nums[0], nums[1]) for i in range(2, len(nums)): dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) return dp[-1]

šŸ’” Pro Tip: Remember, you can only rob one consecutive sequence of houses. So, if the array has an odd number of houses, you cannot rob the middle house and its preceding or succeeding houses.

House Robber II šŸ”

Problem Statement

In House Robber II, we have an array nums and a list of k segments. Each segment represents a group of connected houses, and you can only rob one segment. The goal is to maximize the total amount of money you can steal.

Example

markdown
Input: nums = [2, 3, 2], k = 2 Output: 4 (Rob houses 1 and 3) Input: nums = [1, 2, 3, 1], k = 2 Output: 4 (Rob houses 1, 2, and 3)

Solution

To solve House Robber II, we will first solve House Robber I on the given array. Then, we will subtract the maximum amount of money that can be stolen by excluding the last k-1 houses and include the first house of each segment.

Code Example

python
def rob_segments(nums, k): # Solve House Robber I on the given array total = rob(nums) # Exclude the last k-1 houses and include the first house of each segment excluded = sum(nums[:-k+1]) return total - excluded def rob(nums): if not nums: return 0 if len(nums) == 1: return nums[0] dp = [0] * len(nums) dp[0], dp[1] = nums[0], max(nums[0], nums[1]) for i in range(2, len(nums)): dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) return dp[-1]
Quick Quiz
Question 1 of 1

What is the main difference between House Robber I and House Robber II?

By now, you should have a good understanding of House Robber I and II. These problems are great for learning Dynamic Programming, a fundamental algorithmic technique that you will encounter frequently in your programming journey. Happy coding! šŸ“