logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
2. Add Two Numbers3. Longest Substring Without Repeating Characters5. Longest Palindromic Substring8. String to Integer (atoi)11. Container With Most Water15. 3Sum17. Letter Combinations of a Phone Number19. Remove Nth Node From End of List22. Generate Parentheses33. Search in Rotated Sorted Array34. Find First and Last Position of Element in Sorted Array48. Rotate Image49. Group Anagrams50. Pow(x, n)53. Maximum Subarray54. Spiral Matrix56. Merge Intervals61. Rotate List73. Set Matrix Zeroes74. Search a 2D Matrix75. Sort Colors78. Subsets137. Single Number II138. Copy List with Random Pointer150. Evaluate Reverse Polish Notation151. Reverse Words in a String153. Find Minimum in Rotated Sorted Array155. Min Stack162. Find Peak Element169. Majority Element198. House Robber207. Course Schedule215. Kth Largest Element in an Array227. Basic Calculator II236. Lowest Common Ancestor of a Binary Tree237. Delete Node in a Linked List238. Product of Array Except Self451. Sort Characters By Frequency540. Single Element in a Sorted Array560. Subarray Sum Equals K636. Exclusive Time of Functions875. Koko Eating Bananas1011. Capacity To Ship Packages Within D Days1101. The Earliest Moment When Everyone Become Friends1283. Find the Smallest Divisor Given a Threshold2140. Solving Questions With Brainpower3169. Count Days Without Meetings3481. Apply Substitutions
Code Scratch/leetcode/medium

198. House Robber

1 min read
On this pageIntitution
IntitutionComplexityCode

# Intitution

In the House Robber problem, the challenge is to maximize the amount of money you can steal without robbing two adjacent houses. This forms a classic dynamic programming problem where the decision at each house depends on whether robbing it yields a better total compared to skipping it. The key idea is to make a choice at each step: rob the current house and skip the next, or skip the current house and move to the next one.

# Complexity

Space ComplexityTime Complexity
O(n)\text{O}(n)O(n)O(2n)\text{O}(2^n)O(2n)

# Code

int[] memoizedResults;
 
public int calculateMaxRobbery(int[] houseMoney, int currentIndex) {
    // Base case: No more houses to rob
    if (currentIndex >= houseMoney.length) return 0;
 
    // If already computed, return the stored result
    if (memoizedResults[currentIndex] != -1) return memoizedResults[currentIndex];
 
    // Rob the current house and move to the house after next
    int robThisHouse = houseMoney[currentIndex] + calculateMaxRobbery(houseMoney, currentIndex + 2);
 
    // Skip the current house and check the next one
    int skipThisHouse = calculateMaxRobbery(houseMoney, currentIndex + 1);
 
    // Take the maximum of robbing or skipping the current house
    memoizedResults[currentIndex] = Math.max(robThisHouse, skipThisHouse);
 
    return memoizedResults[currentIndex];
}
 
public int rob(int[] houseMoney) {
    memoizedResults = new int[houseMoney.length];
    Arrays.fill(memoizedResults, -1); // Initialize memoization array with -1
    return calculateMaxRobbery(houseMoney, 0);
}
 
←Previous169. Majority ElementNext→207. Course Schedule

On this page

IntitutionComplexityCode