House Robber - Complete Solution Guide
House Robber is LeetCode problem 198, 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, the only constraint stopping you from robbing each of them is that adjacent houses have security systems 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 money you can rob tonight without alerting the police .
Detailed Explanation
The problem presents a scenario where a professional robber plans to rob houses along a street. Each house contains a certain amount of money. However, adjacent houses have connected security systems, meaning that if two adjacent houses are robbed on the same night, the police will be alerted. The goal is to determine the maximum amount of money the robber can steal without triggering the alarm (i.e., without robbing adjacent houses). The input is an array of integers `nums`, where `nums[i]` represents the amount of money in the i-th house. The output should be the maximum possible amount of money that can be robbed.
Solution Approach
The solution utilizes dynamic programming with space optimization. It maintains two variables, `rob1` and `rob2`, to store the maximum amount of money that can be robbed up to the previous house and up to two houses ago, respectively. For each house, the algorithm checks if it's more profitable to rob the current house (which means we can't rob the previous one, so we add the current house's money to `rob2`) or not rob the current house (in which case we take the maximum we could rob up to the previous house, `rob1`). The algorithm then updates `rob2` to `rob1` and `rob1` to the maximum amount calculated for the current house.
Step-by-Step Algorithm
- Step 1: Initialize two variables, `rob1` and `rob2`, to 0. `rob1` represents the maximum amount of money that can be robbed up to the previous house, and `rob2` represents the maximum amount that can be robbed up to two houses ago.
- Step 2: Iterate through the `nums` array.
- Step 3: For each house `n` in `nums`, calculate the maximum amount that can be robbed by either robbing the current house (n + rob2) or not robbing the current house (rob1). Store this value in a temporary variable `temp`.
- Step 4: Update `rob2` to `rob1` and `rob1` to `temp`. This shifts the 'window' to consider the next house.
- Step 5: After iterating through all the houses, `rob1` will contain the maximum amount of money that can be robbed without robbing adjacent houses. Return `rob1`.
Key Insights
- Insight 1: The optimal solution for robbing up to house `i` depends on the optimal solutions for robbing up to house `i-1` and `i-2`. This suggests a dynamic programming approach.
- Insight 2: We don't need to store the optimal solutions for all houses. Only the last two optimal solutions are required to calculate the current one. This allows for space optimization.
- Insight 3: Consider the base cases: if there's only one house, rob it. If there are two houses, rob the house with more money.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Accenture, Agoda, Airbnb, Arcesium, ByteDance, CARS24, Cisco, Citadel, Cleartrip, DE Shaw, Databricks, Datadog, EPAM Systems, Expedia, Flipkart, Goldman Sachs, Infosys, Intuit, LinkedIn, Morgan Stanley, Nordstrom, Nvidia, PayPal, PhonePe, Salesforce, ServiceNow, Tesla, Turing, Walmart Labs, Zoho.