The House Robber coding problem is a classic optimization task. You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. However, all houses are equipped with a security system: it will automatically contact the police if two adjacent houses are broken into on the same night. Given a list of non-negative integers representing the money in each house, you need to find the maximum amount of money you can rob tonight without alerting the police.
This is one of the most famous Dynamic Programming interview patterns. Companies like Meta, Amazon, and Google use it to see if a candidate can transition from a naive recursive approach to an optimized iterative one. It tests your ability to identify sub-problems and state transitions. If you can solve this, you demonstrate that you understand how to make a series of dependent decisions to reach a global optimum.
This problem is solved using Dynamic Programming (DP). For each house , you have two choices:
dp[i] = max(dp[i-1], dp[i-2] + nums[i]).
You can optimize this to space by only keeping track of the last two values instead of a full DP array.Houses: [1, 2, 3, 1]
[2, 1, 1, 2].Always look for the "Decision" in DP problems. For each item, the question is usually "Should I include this or not?" Once you define the consequences of that choice, the DP state follows naturally.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Product Subarray | Medium | Solve | |
| House Robber II | Medium | Solve | |
| Partition Equal Subset Sum | Medium | Solve | |
| Triangle | Medium | Solve | |
| Coin Change II | Medium | Solve |