The trick here is to name the state correctly, then let the implementation follow. For 213. House Robber II, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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:
- DP
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 rob, dpNoLast, dpNoFirst.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- 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
01//DP
02//Runtime: 0 ms, faster than 100.00% of C++ online submissions for House Robber II.
03//Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for House Robber II.
04class Solution {
05public:
06 int rob(vector<int>& nums) {
07 int N = nums.size();
08 if(N == 0) return 0;
09 if(N <= 3) return *max_element(nums.begin(), nums.end());
10 vector<int> dpA = nums, dpB = nums;
11
12 //0~N-2
13 for(int i = N-3; i >= 0; i--){
14 dpA[i] += max(((i+2 < N-1) ? dpA[i+2] : 0),
15 ((i+3 < N-1) ? dpA[i+3] : 0));
16 }
17
18 //1~N-1
19 for(int i = N-3; i >= 1; i--){
20 dpB[i] += max(((i+2 < N) ? dpB[i+2] : 0),
21 ((i+3 < N) ? dpB[i+3] : 0));
22 }
23
24 return max(max(dpA[0], dpA[1]),
25 max(dpB[1], dpB[2]));
26
27 // return max(dpA[0], dpB[1]);
28 }
29};
30
31//DP, forward
32//Runtime: 0 ms, faster than 100.00% of C++ online submissions for House Robber II.
33//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for House Robber II.
34class Solution {
35public:
36 int rob(vector<int>& nums) {
37 int n = nums.size();
38
39 if(n == 0) return 0;
40 if(n == 1) return nums[0];
41 if(n == 2) return max(nums[0], nums[1]);
42
43 vector<int> dpNoLast(n-1);
44 vector<int> dpNoFirst(n); //padding 0 first
45
46 dpNoLast[0] = nums[0];
47 dpNoLast[1] = max(nums[0], nums[1]);
48
49 for(int i = 2; i < dpNoLast.size(); i++){
50 //if we rob the ith house, we can add dp[i-2] because there is no alear
51 dpNoLast[i] = max(dpNoLast[i-1], dpNoLast[i-2]+nums[i]);
52 }
53
54 //start from index 1
55 //index 0 is just used for padding
56 dpNoFirst[1] = nums[1];
57 dpNoFirst[2] = max(nums[1], nums[2]);
58
59 for(int i = 3; i < dpNoFirst.size(); i++){
60 //if we rob the ith house, we can add dp[i-2] because there is no alear
61 dpNoFirst[i] = max(dpNoFirst[i-1], dpNoFirst[i-2]+nums[i]);
62 }
63
64 return max(dpNoLast.back(), dpNoFirst.back());
65 }
66};
Cost