Leetcode Problem 198. House Robber

198. House Robber

Leetcode Solutions

Optimized Dynamic Programming

  1. Initialize two variables, robNext and robNextPlusOne, to 0 and nums[nums.length - 1] respectively.
  2. Iterate over the array in reverse, starting from the second-to-last element.
  3. For each house i, calculate the maximum profit current by comparing robNext (skip current house) and robNextPlusOne + nums[i] (rob current house).
  4. Update robNextPlusOne to robNext.
  5. Update robNext to current.
  6. After the loop, return robNext as the maximum profit that can be robbed.
UML Thumbnail

Recursion with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...