problem: https://leetcode.com/problems/house-robber-ii/
Multi-state transition dp. My approach is to maintain the four states. Dp twice with the basic idea of the state is more.
class Solution { public: int rob(vector<int>& nums) { int n = nums.size(); vector<int> robber(n + 1, 0); vector<int> norobber(n + 1, 0); vector<int> nofirst_robber(n + 1, 0); vector<int> nofirst_norobber(n + 1, 0); for(int i = 0;i < n;i++) { if(i == 0) { nofirst_robber[i + 1] = nofirst_norobber[i]; nofirst_norobber[i + 1] = max(nofirst_robber[i], nofirst_norobber[i]); } else { nofirst_robber[i + 1] = nofirst_norobber[i] + nums[i]; nofirst_norobber[i + 1] = max(nofirst_robber[i], nofirst_norobber[i]); } if(i == n - 1 && i != 0) { robber[i + 1] = norobber[i]; norobber[i + 1] = max(robber[i], norobber[i]); } else { robber[i + 1] = norobber[i] + nums[i]; norobber[i + 1] = max(robber[i], norobber[i]); } } return max({robber[n], norobber[n], nofirst_robber[n], nofirst_norobber[n]}); } };