← Home

198. House Robber

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
198

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 198. House Robber, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: dynamic programming.

The notes already sitting in the source point us in the right direction:

  • recursion
  • TLE

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are robStart, rob.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//recursion
02//TLE
03/**
04class Solution {
05public:
06    int robStart(vector<int>& nums, int s){
07        if(s >= nums.size()) return 0;
08        return nums[s] + max(robStart(nums, s+2), robStart(nums, s+3));
09    }
10    int rob(vector<int>& nums) {
11        return max(robStart(nums, 0), robStart(nums, 1));
12    }
13};
14**/
15
16//DP, consider 2nd and 3rd next houses
17//Runtime: 4 ms, faster than 100.00% of C++ online submissions for House Robber.
18//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for House Robber.
19
20class Solution {
21public:
22    vector<int> money;
23    int rob(vector<int>& nums) {
24        if(nums.size() == 0) return 0;
25        else if(nums.size() == 1) return nums[0];
26        
27        int N = nums.size();
28        money = nums;
29        //so that we can access money[N]
30        money.push_back(0);
31        for(int i = N-3; i >= 0; i--){
32            money[i] += max(money[i+2], money[i+3]);
33        }
34        return max(money[0], money[1]);
35    }
36};
37
38//DP, consider next 2 houses
39//Runtime: 0 ms, faster than 100.00% of C++ online submissions for House Robber.
40//Memory Usage: 6.4 MB, less than 100.00% of C++ online submissions for House Robber.
41class Solution {
42public:
43    int rob(vector<int>& nums) {
44        int n = nums.size();
45        
46        if(n == 0) return 0;
47        if(n == 1) return nums[0];
48        
49        vector<int> dp(n);
50        
51        dp[0] = nums[0];
52        dp[1] = max(nums[0], nums[1]);
53        
54        for(int i = 2; i < n; i++){
55            //if we rob the ith house, we can add dp[i-2] because there is no alear
56            dp[i] = max(dp[i-1], dp[i-2]+nums[i]);
57        }
58        
59        return dp[n-1];
60    }
61};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.