๐Ÿ—“๏ธ Day 19: House Robber Pattern

๐Ÿ“˜ What Youโ€™ll Learn

  • What is the House Robber pattern?
  • Why you can't rob adjacent houses
  • Applying dynamic programming to maximize profit
  • Translating logic into TypeScript
  • Recursion โ†’ Memoization โ†’ Tabulation
  • Real LeetCode example

๐Ÿง  Concept

The House Robber pattern is a classic dynamic programming scenario. Youโ€™re given an array of non-negative integers representing the amount of money in each house. You cannot rob two adjacent houses, or youโ€™ll trigger the alarm! ๐Ÿšจ๐Ÿ’ธ

Your goal is to pick houses to rob such that the sum of stolen money is maximized, without robbing adjacent houses.

Think: Should I rob this house and skip the next, or skip this one and check the next house's opportunity? ๐Ÿค”


๐Ÿ“Š Visualization

Let's say we have houses with money:

Index:   0   1   2   3   4
Money:  2   7   9   3   1

You can't rob house 1 and 2 together, or 2 and 3. So you calculate max loot at each step:

dp[i] = max(dp[i-1], dp[i-2] + nums[i])

๐Ÿ’ป TypeScript Examples

โŒ Recursive Brute-force

function rob(nums: number[]): number {
  function helper(i: number): number {
    if (i >= nums.length) return 0;
    return Math.max(
      helper(i + 1),              // skip this house
      nums[i] + helper(i + 2)     // rob this house
    );
  }
  return helper(0);
}
  • โฑ๏ธ Time: O(2โฟ) โ€” too slow!

โœ… Top-Down Memoization

function rob(nums: number[]): number {
  const memo: Record<number, number> = {};
  function dp(i: number): number {
    if (i >= nums.length) return 0;
    if (i in memo) return memo[i];
    memo[i] = Math.max(dp(i + 1), nums[i] + dp(i + 2));
    return memo[i];
  }
  return dp(0);
}
  • โฑ๏ธ Time: O(n)
  • ๐Ÿ’พ Space: O(n) (call stack + memo)

โœ… Bottom-Up Tabulation

function rob(nums: number[]): number {
  if (nums.length === 0) return 0;
  if (nums.length === 1) return nums[0];
  const dp: number[] = [];
  dp[0] = nums[0];
  dp[1] = Math.max(nums[0], nums[1]);
  for (let i = 2; i < nums.length; i++) {
    dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
  }
  return dp[nums.length - 1];
}
  • โฑ๏ธ Time: O(n)
  • ๐Ÿ’พ Space: O(n)

๐Ÿ’ก Optimized DP (Constant Space)

function rob(nums: number[]): number {
  let prev1 = 0, prev2 = 0;
  for (let num of nums) {
    let temp = Math.max(prev1, prev2 + num);
    prev2 = prev1;
    prev1 = temp;
  }
  return prev1;
}
  • โฑ๏ธ Time: O(n)
  • ๐Ÿ’พ Space: O(1) ๐Ÿš€

๐Ÿงช LeetCode Homework

๐Ÿ“˜ 198. House Robber

๐Ÿงฉ You are a professional robber. Each house has a certain amount of money. Adjacent houses have security systems connected โ€” robbing two in a row triggers an alarm. Return the maximum amount you can rob.

function rob(nums: number[]): number {
  let prev1 = 0, prev2 = 0;
  for (let num of nums) {
    let temp = Math.max(prev1, prev2 + num);
    prev2 = prev1;
    prev1 = temp;
  }
  return prev1;
}

const houses = [2,7,9,3,1];
console.log(rob(houses)); // 12

๐Ÿ“Œ Key Takeaways

  • Robbing adjacent houses = ๐Ÿšจ
  • Use DP to track max amount without violating rules
  • Learn to convert recursive โ†’ memoization โ†’ tabulation โ†’ optimized
  • This pattern appears in more advanced problems (e.g., circular houses, trees)

๐Ÿ”ฎ Up Next

โ–ถ๏ธ Day 20: House Robber II (Circular Houses)

This page was last edited on 2025-08-01 08:51

Powered by Wiki|Docs

This page was last edited on 2025-08-01 08:51

Tri Nguyen
No Copyright

Powered by Wiki|Docs