Solved LeetCode 198 House Robber with DP and Space Optimization

🎯 LeetCode 198 – House Robber | Dynamic Programming Today I revisited one of the most popular DP questions on LeetCode: House Robber. At first glance, it looks like a simple array problem — but it actually introduces one of the most important DP patterns: 👉 “Take or Skip” optimization using previous states. 🔍 Problem Insight You are given an array where each element represents the money in a house. The catch? You cannot rob two adjacent houses, or the alarm goes off. So for every house, we make a decision: ✔️ Rob this house → add its money + best answer from non-adjacent house ✔️ Skip this house → carry forward the best previous result This leads to the core DP transition: curr = max(curr, prev + nums[i]) 🧠 Why I Loved This Problem No need for a DP array Can be solved using only two variables Shows the power of optimizing space Very common interview pattern Helps understand Fibonacci-like transitions ✔️ My Java Solution (O(n) time, O(1) space) int prev = 0, curr = 0; for (int n : nums) { int temp = curr; curr = Math.max(curr, prev + n); prev = temp; } return curr; 🚀 Key Takeaways Many DP problems are just “previous state management”. Space optimization often turns complex-looking problems into simple ones. Always look for patterns like: pick / not pick take / skip include / exclude Happy to have solved and understood this one deeply — moving on to the next DP challenge! 💪🔥 #LeetCode #DynamicProgramming #JavaProgramming #CodingJourney #ProblemSolving #DSA #TechLearning #100DaysOfCode

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories