Advertisement

House Robber II - LeetCode 213 Solution

House Robber II - Complete Solution Guide

House Robber II is LeetCode problem 213, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system 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

Detailed Explanation

The House Robber II problem extends the original House Robber problem by introducing a circular arrangement of houses. Given an array `nums` representing the amount of money in each house, the goal is to find the maximum amount of money you can rob without robbing adjacent houses. The key difference from House Robber I is that the first and last houses are now considered neighbors, creating a circular dependency. This means you cannot rob both the first and last houses simultaneously.

Solution Approach

The solution addresses the circular dependency by splitting the problem into two subproblems, each representing a linear arrangement of houses. In the first subproblem, the last house is excluded, and in the second subproblem, the first house is excluded. This ensures that the first and last houses are not robbed simultaneously. The maximum robbery amount is then calculated for each of these linear arrangements using dynamic programming. The algorithm iterates through each house in the given range. At each step, it updates the maximum amounts that can be robbed so far, ensuring no two adjacent houses are robbed.

Step-by-Step Algorithm

  1. Step 1: Handle the base case: If the input array contains only one house, return the amount in that house.
  2. Step 2: Define a helper function `_rob_linear(arr)` (or equivalent in other languages) to solve the House Robber problem for a linear array. This function uses dynamic programming with two variables, `rob1` and `rob2`, to store the maximum amount robbed up to the previous house and the current house, respectively.
  3. Step 3: Calculate the maximum robbery amount for the subproblem excluding the last house: Call `_rob_linear(nums[:-1])` or the equivalent `_rob_linear(nums, 0, n-2)` in Java.
  4. Step 4: Calculate the maximum robbery amount for the subproblem excluding the first house: Call `_rob_linear(nums[1:])` or the equivalent `_rob_linear(nums, 1, n-1)` in Java.
  5. Step 5: Return the maximum of the two robbery amounts calculated in steps 3 and 4.

Key Insights

  • Insight 1: Breaking the circle: The circular arrangement can be handled by considering two separate scenarios: one where you rob houses from index 0 to n-2 (excluding the last house) and another where you rob houses from index 1 to n-1 (excluding the first house).
  • Insight 2: Dynamic Programming: The linear robbing problem (robbing houses in a straight line) can be efficiently solved using dynamic programming. The core idea is to keep track of two values: the maximum amount that can be robbed including the current house and the maximum amount that can be robbed excluding the current house.
  • Insight 3: Constant Space: The dynamic programming approach can be optimized to use constant space by storing only the previous two maximum values.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming.

Companies

Asked at: ByteDance, Databricks, Datadog, Docusign, LinkedIn, Nordstrom, PhonePe, Visa, thoughtspot.