House Robber in Python and Java
In this tutorial, we will learn LeetCode #198: House Robber in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the House Robber Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use dynamic programming because it gives a clean and optimized solution.
Example input: nums = [3, 10, 3, 1, 8] Expected result: Output: 18 Explanation: Rob houses with money 10 and 8.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: nums = [3, 10, 3, 1, 8] Approach: dynamic programming We update variables step by step until we reach: Output: 18
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why dynamic programming is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1 Use this input. nums = [3, 10, 3, 1, 8] At every house, we choose rob or skip. Step 2 At house with money 3: rob = 3 skip = 0 Best so far = 3 Step 3 At house with money 10, we cannot rob previous house and this house together. rob current = previous skip + 10 rob current = 0 + 10 = 10 skip current = max(previous rob, previous skip) skip current = max(3, 0) = 3 Step 4 At the last house with money 8, the best previous non-adjacent value lets us get 18. Best answer = 18
Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output: 18
Example 1
Input: nums = [3, 10, 3, 1, 8] Output: Output: 18 Explanation: Rob houses with money 10 and 8.
Example 2
Input: nums = [6, 2, 9, 3, 1] Output: 16 Explanation: Rob houses with 6, 9, and 1.
Python Code
Here is the complete Python solution for LeetCode #198.
class HouseMoneyRobber:
def rob(self, nums):
rob_previous = 0
skip_previous = 0
for money in nums:
new_rob = skip_previous + money
new_skip = max(skip_previous, rob_previous)
rob_previous = new_rob
skip_previous = new_skip
return max(rob_previous, skip_previous)
robber = HouseMoneyRobber()
print(robber.rob([3, 10, 3, 1, 8])) # Output: 18
print(robber.rob([2, 7, 9, 3, 1])) # Output: 12Java Code
Here is the complete Java solution for LeetCode #198.
class HouseMoneyRobber {
public int rob(int[] nums) {
int robPrevious = 0;
int skipPrevious = 0;
for (int money : nums) {
int newRob = skipPrevious + money;
int newSkip = Math.max(skipPrevious, robPrevious);
robPrevious = newRob;
skipPrevious = newSkip;
}
return Math.max(robPrevious, skipPrevious);
}
public static void main(String[] args) {
HouseMoneyRobber robber = new HouseMoneyRobber();
System.out.println(robber.rob(new int[] {3, 10, 3, 1, 8})); // Output: 18
System.out.println(robber.rob(new int[] {2, 7, 9, 3, 1})); // Output: 12
}
}Time and Space Complexity
Time Complexity: O(n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(1)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #198: House Robber becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.